How to setup a scheduled task in Ubuntu

Hello,

i need to setup a scheduled task to automatically run the following command every hour:

perl '/myscripts/script.pl'

Any help is very appreciated

Thanks

Sal

7 Replies

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

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

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:

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.

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

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

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.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct