LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-22-2011, 10:58 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

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


Hello,

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

Code:
# cat /etc/crontab
...
*/1 * * * * root /tmp/myscript.sh

# 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
d072330
Member
 
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
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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:
Code:
* * * * * root /tmp/myscript.sh
should work to run /tmp/myscript.sh 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"

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

EDIT2:
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
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In alternative, if you want to update the user's crontab through script you can simply do:
Code:
#!/bin/bash
(crontab -l 2>/dev/null; echo "* * * * * /tmp/myscript.sh") | 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
benq70
LQ Newbie
 
Registered: Jun 2003
Distribution: CentOS, Ubuntu
Posts: 6

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

Then just modify /tmp/myscript.sh 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/myscript.sh 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
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration