-   Linux - Server (
-   -   Crontab entry to shutdown if no users (

LostDakota 07-01-2009 12:31 AM

Crontab entry to shutdown if no users

I have an Ubuntu 8.04 server that supplies gnump3d tunes, some http and serves as a test bed into my somewhat recent fascination with Linux. I love the flexibility of the platform and really enjoy rocking the CLI.

My question is: How can I write a script that will check to see if I am logged in and if it returns negative, shutdown the server.

I have tried (and forgive my ignorance, please. I have witnessed the awesome power of people helping with Linux and I must say it is amazing):



if [ users=drew ]
      echo You are logged in
      shutdown -P now

Now, as I understand, when I add this to /etc/crontab, It needs to be run by a user so I tried:


30 23 * * * * root NightKill
Whereas 'NightKill' is the script name.

If I have an extra *, disregard it. I wasn't copying from my actual crontab. How does one get this to work? I have googled, I have searched, I am new. Any help is appreciated.


colucix 07-01-2009 03:52 AM

I don't really understand what the problem is. The syntax of your script is clearly wrong because it lacks the "command substitution". Anyway, you can test if a particular user is logged in, by means of the w command, e.g.

if [ -z "$(w -h $user)" ]
  echo user $user is not logged in
  echo user $user is logged in

Then just add your script to the root's crontab. Note that you have to use the full path of the /sbin/shutdown command, because of the limited environment of cron (the PATH is limited to /bin:/usr/bin).

JZL240I-U 07-01-2009 08:30 AM

Have a look at these:


LostDakota 07-01-2009 10:47 AM

Thank you colucix and JZL240I-U.

I guess I was trying to use the output from the users command to test if I was logged in. I should have been more clear. JZL240I-U I have made it about half way through the first two sites you mentioned, I must just need to pay more attention.

Thanks again


All times are GMT -5. The time now is 10:24 AM.