Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 02-22-2011, 10:58 AM   #1
Registered: Aug 2008
Posts: 528

Rep: Reputation: 32
Question [cron] Adding job through script?


I need to add a job to contrab from a script. Appending the following line to /etc/crontab doesn't do anything:

# cat /etc/crontab
*/1 * * * * root /tmp/

# crontab -l
no crontab for root
Is there a way to automate adding jobs to cron?

Thank you.
Old 02-22-2011, 11:20 AM   #2
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Rep: Reputation: 6
Easiest way is to just type crontab -e as the user you want the cron job to run as and then enter your script and then save it. Then your crontab -l should list your job.

Last edited by d072330; 02-22-2011 at 11:23 AM.
Old 02-22-2011, 11:27 AM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Two things:

1. Your use of "*/1" is unnecessary. Unless I'm mistaken, all you need is the asterisk.

2. /etc/crontab is not root's crontab. The root user has a user-based crontab just like any other user. The /etc/crontab file and the files in /etc/cron.d are system-wide crontab files that are not associated with a specific user--they are generic "system" crontabs. They do not require use of the crontab command to install or modify.

So, appending a line to /etc/crontab, such as:
* * * * * root /tmp/
should work to run /tmp/ as root once every minute.

Note, you will not be able to see the job listed under root or any other user's crontab by using "crontab -l"

If you do not believe the script is running, then check that your script uses absolute paths to programs and/or have the script write information to a log file to verify it is working.

You can read more by using man 5 crontab -- the comments in the example crontab files explain that /etc/crontab is a "system" crontab file.

Last edited by Dark_Helmet; 02-22-2011 at 11:36 AM.
Old 02-22-2011, 11:34 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In alternative, if you want to update the user's crontab through script you can simply do:
(crontab -l 2>/dev/null; echo "* * * * * /tmp/") | crontab -
where redirection of standard error to /dev/null prevents the printing out of "no crontab for root" message if it is initially empty.
Old 02-22-2011, 02:05 PM   #5
LQ Newbie
Registered: Jun 2003
Distribution: CentOS, Ubuntu
Posts: 6

Rep: Reputation: 1
Proceed with adding your cron entry: * * * * * /tmp/

Then just modify /tmp/ to do your bidding. That way you don't need to touch your cron afterwards.

If you need to verify that your script was executed look in /var/log/cron(or equivalent) or execute "touch /tmp/myscript.`date +%m-%d-%y-%H-%M-%S`" within /tmp/ or something similar.

Long term I usually have my scripts send output to a log file for troubleshooting purposes like "echo blah blah >>/home/me/logs/logfile.log"

Hope this helps.
Old 02-28-2011, 06:31 AM   #6
Registered: Aug 2008
Posts: 528

Original Poster
Rep: Reputation: 32
Thanks for the help.


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
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
script does not run in cron job kashyapvirgo Linux - General 8 03-20-2007 10:55 AM
Cron Job with a Script bravored Linux - General 4 08-05-2005 10:27 AM
cron job for expect script ruchitadesai Programming 9 05-27-2005 04:34 AM
adding new cron job via web Bake-SaleNet Linux - Software 3 04-07-2004 06:31 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:57 PM.

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