LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 06-30-2009, 11:31 PM   #1
LostDakota
LQ Newbie
 
Registered: Jun 2009
Location: OHIO
Distribution: Ubuntu 8.04 Server, Ubuntu 9.04 Desktop
Posts: 2

Rep: Reputation: 0
Crontab entry to shutdown if no users


Hello,

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):

Code:
#!/bin/bash

if [ users=drew ]
   then 
       echo You are logged in
   else
       shutdown -P now
fi
Now, as I understand, when I add this to /etc/crontab, It needs to be run by a user so I tried:

Code:
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.

/drew
 
Old 07-01-2009, 02:52 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
Code:
#!/bin/bash
user=drew
if [ -z "$(w -h $user)" ]
then
  echo user $user is not logged in
else
  echo user $user is logged in
fi
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).
 
Old 07-01-2009, 07:30 AM   #3
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,642

Rep: Reputation: Disabled
Have a look at these:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/guides.html#abs
http://theory.uwinnipeg.ca/UNIXhelp/

HTH.
 
Old 07-01-2009, 09:47 AM   #4
LostDakota
LQ Newbie
 
Registered: Jun 2009
Location: OHIO
Distribution: Ubuntu 8.04 Server, Ubuntu 9.04 Desktop
Posts: 2

Original Poster
Rep: Reputation: 0
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

/drew
 
  


Reply

Tags
bash, crontab, script, shutdown, users


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about crontab entry Ravi kumar Linux - Software 2 09-16-2008 01:41 PM
Somehow not getting crontab entry right Jykke Linux - Desktop 4 02-29-2008 01:50 PM
Crontab entry doubt paventhan Solaris / OpenSolaris 1 11-03-2007 05:33 AM
crontab entry? Master Fox Linux - Software 4 08-23-2005 01:20 PM
crontab entry pilipk01 Linux - General 4 11-19-2003 07:53 PM


All times are GMT -5. The time now is 08:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration