| Author |
Message |
djmicron
Joined: 21 Jun 2008
Posts: 5
|
| Posted: Tue Jun 24, 2008 6:52 am Post subject: How to setup a scheduled task in Ubuntu |
|
|
Hello,
i need to setup a scheduled task to automatically run the following command every hour:
Code: perl '/myscripts/script.pl'
Any help is very appreciated
Thanks
Sal |
|
| Back to top |
|
fendrish78
Joined: 30 May 2008
Posts: 8
|
| Posted: Tue Jun 24, 2008 7:09 am Post subject: |
|
|
You would want to use cron to accomplish this.
As the user that you want to run the command as you would run:
crontab -e
This will bring up an editor. Put the following in the editor and save it.
0 * * * * perl '/myscripts/script.pl'
This will run the command ever hour at 0 minutes of that hour.
--Sean |
|
| Back to top |
|
djmicron
Joined: 21 Jun 2008
Posts: 5
|
| Posted: Tue Jun 24, 2008 8:04 am Post subject: |
|
|
Thank You very much.
Sal |
|
| Back to top |
|
djmicron
Joined: 21 Jun 2008
Posts: 5
|
| Posted: Tue Jun 24, 2008 1:03 pm Post subject: |
|
|
Hello,
even if the task has been added to the edited crontab file, it is not running.
I have verified that the command i have added is correct by running it in terminal and it works, but i don't know what i can do to let the cron run by itself.
I see that when i run the crontab -e command it opens a file named "crontab" which contains the command i have added before, but i'm not sure if it has been saved in the right location, because if i open the file /etc/crontab it does not contain the command i have added.
Thank You
Sal |
|
| Back to top |
|
SteveG
Joined: 30 Nov 2003
Posts: 212
|
| Posted: Tue Jun 24, 2008 1:10 pm Post subject: |
|
|
Sal,. /etc/crontab is the system level file, and you wouldn't expect to see your changes there. User crontabs are in /var/spool/cron/crontabs, but you won't be able to read that as a user, just as root, and you definitely don't want to edit them directly.
Try 'crontab -l' to list your user cron file. Using 'crontab -e' should create copy of what is in /var/spool/cron/crontab, and, when you save, put it back in place. You should see a message in the console "crontab: installing new crontab", and, if you look in /var/log/cron.log, a sequence like this:
Code: Jun 24 13:06:28 speedy crontab[11270]: (steveg) BEGIN EDIT (steveg)
Jun 24 13:06:35 speedy crontab[11270]: (steveg) REPLACE (steveg)
Jun 24 13:06:35 speedy crontab[11270]: (steveg) END EDIT (steveg)
Jun 24 13:07:01 speedy /usr/sbin/cron[2833]: (steveg) RELOAD (crontabs/steveg)
If there's a problem, you should see an error message. |
|
| Back to top |
|
djmicron
Joined: 21 Jun 2008
Posts: 5
|
| Posted: Tue Jun 24, 2008 2:38 pm Post subject: |
|
|
thank You Steve,
i have done 'crontab -l' and verified that cron job has been added to that file, but i don't see the cron.log inside the /var/log/ , so i think there is something wrong with cron on my ubuntu installation.
Is there some command i can run to force cron to start?
Thank You
Sal |
|
| Back to top |
|
djmicron
Joined: 21 Jun 2008
Posts: 5
|
| Posted: Tue Jun 24, 2008 3:43 pm Post subject: |
|
|
i have done a research and it seems that it's not possible to run cron tasks as root user and it is what i tried to do, so i have added the task as normal user and it worked out.
Thank You
Sal |
|
| Back to top |
|
SteveG
Joined: 30 Nov 2003
Posts: 212
|
| Posted: Tue Jun 24, 2008 4:09 pm Post subject: |
|
|
| If you ever do need to add a root cronjob (or any other system account cron job), don't mess with /etc/crontab. Either add the script to one of /etc/cron.{daily,weekly,monthly), where it will run as root, or add a crontab fragment to /etc/cron.d. These fragments include a username between the time-spec and the actual command. See crontab(5) for details. |
|
| Back to top |
|
| |