LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   How do i run a command every day at a certain time automatically? (http://www.linuxquestions.org/questions/linux-server-73/how-do-i-run-a-command-every-day-at-a-certain-time-automatically-911766/)

Squerl101 11-03-2011 10:09 PM

How do i run a command every day at a certain time automatically?
 
Hey guys.

I am a noob. I just want to know how to make a command automatically run EVERY DAY at a certain time. I want it to run /sbin/shutdown -h 22:00. This needs to run everyday at 10:00PM. Can someone tell me how i can make it do it automatically? Thanks.

jefro 11-03-2011 10:16 PM

Hello.

I think most systems support a thing called cron. Cron can get a bit tricky but if you remember cron is sort of like a user with it's own rights and authentication then you are OK. You want to run that script before to be sure it runs.

Squerl101 11-03-2011 10:20 PM

Thanks, but what do i have to enter into the terminal to have it shutdown everyday at 10:00?

jlinkels 11-03-2011 10:56 PM

As root, or after sudo, edit /etc/crontab.

Enter this line at the end:
Code:

0 22 * * * root shutdown -h now
Make sure you have an empty line as last line!

If you are a GUI only person, you can edit a file as root by pressing CTL-F2 and tell that you want to run an application as root, and then choose you fav editor

jlinkels

sswuste 11-04-2011 06:14 AM

You might want to specify the complete path to the shutdown command in your cronjob: /sbin/shutdown

fukawi1 11-04-2011 06:21 AM

Quote:

Originally Posted by jefro (Post 4515375)
Hello.

I think most systems support a thing called cron. Cron can get a bit tricky but if you remember cron is sort of like a user with it's own rights and authentication then you are OK. You want to run that script before to be sure it runs.

Its my understanding, that if you run
Code:

crontab -e
as any user, then it creates a crontab for that user, and anything runs as that user.. At least, that is how it appears to be for my FC15 box. Am I correct in my understanding?

Note: for a shutdown command, it would need to be done as root, as stated in other replies. Unless passwordless shutdown has been enabled in sudoers.

jlinkels 11-04-2011 06:39 AM

Quote:

Originally Posted by fukawi1 (Post 4515594)
Its my understanding, that if you run
Code:

crontab -e
as any user, then it creates a crontab for that user, and anything runs as that user.. At least, that is how it appears to be for my FC15 box. Am I correct in my understanding?

That is correct, but this command opens the default editor which is in most cases vi, and that could be surprising for users who don't how to use or get out of vi. crontab -e is the recommended and only correct way to edit a personal crontab from the command line though.

The shutdown command has to be run as root, and it really fits better in the system-wide /etc/crontab. If you are right and shutdown can be run by a member of the sudoers group a personal crontab might work, but it makes the system less transparent. You don't want to go through a dozen crontabs to see who is responsible for shutting down the machine.

jlinkels


All times are GMT -5. The time now is 12:30 AM.