PDA

View Full Version : sh script with if statement to shutdown



Jonny87
January 21st, 2011, 10:50 AM
I have a script that I using to run some tasks such as back ups, I also want to have the comp turn itself on early in the morn and schedule the script to run at that time when no-one is using the comp.

I want to add a shutdown command to the end of the script so that it shuts down the comp when its done, however I don't want it to shut down if someone is logged on (or accessing a samba shared folder).

Is anyone able to give me an example of how this might be done if it is possible?

fct
January 21st, 2011, 10:56 AM
This should give you the number of currently logged on users:

who | wc -l

luvshines
January 21st, 2011, 05:25 PM
You may want to explore 'at' command
It has some interesting options.
I haven't used it myself, but looks like in your case it might be helpful

Jonny87
January 22nd, 2011, 09:10 AM
This should give you the number of currently logged on users:

who | wc -l

Any idea how would use that with the shutdown command in a script? I'm guessing that it would possibly be with an "if statement" but my I'm still learning scripts and not to familiar with the "if statement" as I've hardly use it. could you possibly give me an example?

hakermania
January 22nd, 2011, 09:44 AM
Any idea how would use that with the shutdown command in a script? I'm guessing that it would possibly be with an "if statement" but my I'm still learning scripts and not to familiar with the "if statement" as I've hardly use it. could you possibly give me an example?

if [ $(who | wc -l) -eq 0 ]; then
#there is none user logged in, so proceed with shutdown :)
#for example dbus-send --print-reply --system --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.S hutdown
mplampla
fi
Not-tested (this will work if he command "who" has no output if none user is logged in)

fct
January 22nd, 2011, 11:43 AM
Any idea how would use that with the shutdown command in a script? I'm guessing that it would possibly be with an "if statement" but my I'm still learning scripts and not to familiar with the "if statement" as I've hardly use it. could you possibly give me an example?

Then you should read some tutorials. I suggest this one:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html