WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Inactivity Timed Shutdown in Shell  (Read 825 times)

Offline Boss 429

  • WikiUser
  • *
  • Posts: 19
Re: Inactivity Timed Shutdown in Shell
« Reply #15 on: May 24, 2017, 05:04:18 AM »
This is the error I'm seeing when trying to run it.

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 9282
Re: Inactivity Timed Shutdown in Shell
« Reply #16 on: May 24, 2017, 05:15:49 AM »
You need to download the extension first with "tce-load -wil procps" or "tce-load -wil procps-ng"

Offline Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 5313
Re: Inactivity Timed Shutdown in Shell
« Reply #17 on: May 24, 2017, 05:20:19 AM »
Hi Boss 429
when I try to run

Code: [Select]
tce-load -I procps
I'm getting a not found error. Is there another package I need to have downloaded prior to trying to run that code?
That should be a lower case  i  in that command. Also, did you first run:
Code: [Select]
tce-load -w procps

Offline Boss 429

  • WikiUser
  • *
  • Posts: 19
Re: Inactivity Timed Shutdown in Shell
« Reply #18 on: May 24, 2017, 06:09:13 AM »
I was able to download it by using:

Code: [Select]
tce-load -wil procps-ng
I did try:

Code: [Select]
tce-load -w procps
and that did not work. It gave me an error saying file not found.

Now that I have it loaded and I can watch the idle timer, would I use this code provided by Misalf to set how long it should be idle before shutdown?

Maybe something like
Code: [Select]
w -hs | awk '/tty1/ {print $3}'

Online Misalf

  • Hero Member
  • *****
  • Posts: 1528
Re: Inactivity Timed Shutdown in Shell
« Reply #19 on: May 25, 2017, 07:52:26 AM »
Converting the format  w  provides to something useful for a script (i.e. print seconds) is kinda tricky, but I found a start point here: https://www.reddit.com/r/linux/comments/2z7oxg/linux_w_command_get_idle_time_in_seconds/

I have modified it so it only outputs the lowest value
Code: [Select]
#!/bin/sh
# Prints idle time in seconds.

f_get_idle() {
    w -hs | awk '{
    if($3 ~ /days/){
        split($3,d,"days");
        print d[1]*86400
    }
    else if($3 ~ /:|s/){
        if ($3 ~/s/) {sub(/s/,"",$3); split($3,s,"."); print s[1]}
        else if ($3 ~/m/) {split($3,m,":"); print (m[1]*60+m[2])*60}
        else {split($3,m,":"); print m[1]*60+m[2]}
    }
    else {print $3}
    }'
}

for i in $(f_get_idle) ; do
    [ -z $IDLE ] && IDLE=$i
    [ $i -lt $IDLE ] && IDLE=$i
done

echo "$IDLE"

Download a copy and keep it handy: Core book ;)

Offline Boss 429

  • WikiUser
  • *
  • Posts: 19
Re: Inactivity Timed Shutdown in Shell
« Reply #20 on: July 18, 2017, 11:54:48 AM »
I apologize for the late reply, I've been caught up in other matters. I've recently had some time to dig back into this. I'm trying to take the code you provided and make it work for what I need, but I may be going about it the wrong way. I am trying to take the lowest output value to use in a condition that would be triggered once it hits a certain time (60 seconds in the test environment).

I have it set-up like this:

Code: [Select]
#!/bin/sh
# Prints idle time in seconds.

f_get_idle() {
    w -hs | awk '{
    if($3 ~ /days/){
        split($3,d,"days");
        print d[1]*86400
    }
    else if($3 ~ /:|s/){
        if ($3 ~/s/) {sub(/s/,"",$3); split($3,s,"."); print s[1]}
        else if ($3 ~/m/) {split($3,m,":"); print (m[1]*60+m[2])*60}
        else {split($3,m,":"); print m[1]*60+m[2]}
    }
    else {print $3}
    }'
}

for i in $(f_get_idle) ; do
    [ -z $IDLE ] && IDLE=$i
    [ $i -lt $IDLE ] && IDLE=$i
done

if ("$IDLE">60)
then
sudo poweroff
fi

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 9472
Re: Inactivity Timed Shutdown in Shell
« Reply #21 on: July 19, 2017, 12:00:47 AM »
Shell tests use square brackets, and > is a string operator. Try
Code: [Select]
if [ "$IDLE" -gt 60 ]; then
The only barriers that can stop you are the ones you create yourself.

Offline Boss 429

  • WikiUser
  • *
  • Posts: 19
Re: Inactivity Timed Shutdown in Shell
« Reply #22 on: July 21, 2017, 09:56:12 AM »
I tried what you suggested, and I'm no longer getting any errors, but it's still not powering off when it should be. Is there a way to monitor the script and see where the issue is at?