Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to edit Crontab 1

Status
Not open for further replies.

HestonJames

Programmer
Aug 11, 2008
187
0
0
GB
Morning All,

I'm pretty much a total novice when it comes to shell scripting but hopefully you guys will be able to best advise me on how to achieve this.

I'm working on putting together a postinst script for a .deb package that gets run after files have been placed in locations around the system to finalize the installation.

For this particular application I need to make an entry into the root users crontab file. Now, on its own this might not seem like a very tricky task, the file is located:

/var/spool/cron/crontabs/root

and looks something like this:

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.Wk9Ybr/crontab installed on Fri Sep 12 10:47:41 2008)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# m h dom mon dow command

The line I want to add to this crontab looks something like this:

* * * * * python /myapp/myapp.py > /myapp/output 2>&1

Now, the problem here lies in the fact that editing the file isnt actualy enough to install the modified crontab onto the system. Usualy, if doing this manualy you would open up the crontab using 'crontab -e' and make your changes, then when existing 'crontab -e' it installs the crontab for you.

Has anyone got any experiance of scripting crontab entries? Ideally I would also need a script which would reverse the entry and remove it, for uninstallation of the package.

I look forward to your suggestions guys,

Many thanks,

Heston
 
Hi Ken,

No worries at all my man, I didnt know how to use cron properly obviously which got us all off on the wrong foot, seems we've all learned a little something :)

At the moment my cron entry looks exactly like this:

Code:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
* * * * *   root   python /myapp/myapp.py > /myapp/output 2>&1

Now, I see the syslog entry which says its reloaing my file after I've changed it however it doesnt appear to be running at all, I dont get any output into /myapp/output as I would when scheduling in the normal manor.

I'm all open to ideas on whats playing up with this.

Cheers all,

Heston
 
Did your app ran correctly when you had the line in root's crontab? If it did, what are the environment variable settings in root's crontab?

Cron sends mail when your command produces output or when there's a problem. In this case, it's going to send to root on your box (which may be aliased to another user); you can change that by setting the MAILTO environment variable. Find where the mail is going, then check to see if you get any clues.

To that end, try removing the [tt]2>&1[/tt] from your command; if your application is producing errors that indicate it's having a problem, this will allow them to be mailed to you.
 
Chipper, thanks for that.

It did run when it was in roots crontab yes, however I have no idea what environment variables are being used by that as I didnt define them. How can I find out which it is using?

The ones in my example above are the same as if I type 'env' from the command line when logged in as ROOT.

Cheers mate,

Heston
 
Oops. Need an edit button...

Replace "Did your app ran" with "Did your app run
 
I meant the environment variables (if any) defined at the top of root's crontab. That is, the environment variables defined in [tt]/var/spool/cron/crontabs/root[/tt].
 
chipper,

Thanks for the reply. There are no environment variables at all defined in the root crontab, it simply looks like this:

Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.5Qp3Kk/crontab installed on Fri Oct  3 20:45:11 2008)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# m h  dom mon dow   command

Thats it :)

Cheers mate,

Heston
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top