| | |
How do I? Edit Crontab using a Script?
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
wow, finally have time from Tech Support to start back on my script! I hope I can get this done sometime soon so I do not have to do all the upgrades by hand. I got some more questions for you pro's! 

•
•
•
•
Shell Scripting Syntax (Toggle Plain Text)
shell prompt> crontab -e :g/watchdog/s/^/#/<ENTER> :wq!<ENTER> shell prompt>
Thank you for this!!! This explains a lot!
Shell Scripting Syntax (Toggle Plain Text)
(echo -e ^[:g/watchdog/s/^/#/\r:wq!\r)|crontab -e
I was not able to get this to run. Every time I used echo I usually start the line with it not "(". Shouldn't I be able to paste this in the command prompt? When I do I get
Shell Scripting Syntax (Toggle Plain Text)
bash: !\r: event not found
Also when I run it within the script I get this:
Shell Scripting Syntax (Toggle Plain Text)
./mysql_upgrade: line 20: syntax error near unexpected token `echo' ./mysql_upgrade: line 20: `$ (echo -e ^[:g/watchdog/s/^/#/\r:wq!\r)|crontab -e'
Also I noticed if watchdog was already stopped ("#") then it adds another "#" to the line. My easy fix would be to run the line to remove "#" twice to make sure. Is this the best way?
Thanks for everything guys! This is actually getting fun!
•
•
•
•
Shell Scripting Syntax (Toggle Plain Text)
crontab -l >/tmp/crontab.a sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1' /tmp/crontab.a | crontab rm /tmp/crontab.a
I tried this as well.
Just trying to comment it out first off and here is what I get out of the command prompt:
Shell Scripting Syntax (Toggle Plain Text)
# ./mysql_upgrade sed: -e expression #1, char 44: unterminated `s' command crontab: usage error: file name must be specified for replace usage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (edit user's crontab) -l (list user's crontab) -r (delete user's crontab)
Any help on this one? If I can get either or to work that would be great!
Also using this method:
Shell Scripting Syntax (Toggle Plain Text)
crontab -l >/tmp/crontab.a
It adds 3 extra lines to the top of crontab.a
Shell Scripting Syntax (Toggle Plain Text)
# DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.XXXXYMQNRs installed on Thu Mar 5 16:10:14 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) USER=root JAVA_HOME=/usr/java/latest 0 9 * * * /etc/webmin/cron/tempdelete.pl */5 * * * * /opt/watchdog/startupWatchdog.sh
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
The following should do the trick quite easily.
To comment out the line:
Shell Scripting Syntax (Toggle Plain Text)
crontab -l |sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1=' | crontab -
To uncomment the line:
Shell Scripting Syntax (Toggle Plain Text)
crontab -l | sed -e 's=^#\(.*/opt/watchdog/startwatchdog.sh$\)=\1=' | crontab -

It appears you don't have a choice when it comes to the first three lines of comments. Crontab() puts them in whether you like it or not. But you can safely ignore them; they're only comments.
•
•
•
•
Forty lashes with a wet noodle for me for posting the wrong thing. The code above should work now; I've reduced it to a single line. This time I actually tested them.
It appears you don't have a choice when it comes to the first three lines of comments. Crontab() puts them in whether you like it or not. But you can safely ignore them; they're only comments.
I still was not able to get it to work. Are you using "Backspace" or "Delete" ? Delete is only way to get rid of the "#"
Here's my crontab:
Shell Scripting Syntax (Toggle Plain Text)
USER=root JAVA_HOME=/usr/java/latest 59 20 * * * /etc/webmin/cron/tempdelete.pl #/5 * * * * /opt/watchdog/startupWatchdog.sh
Heres crontab after "uncomment" code:
Shell Scripting Syntax (Toggle Plain Text)
# DO NOT EDIT THIS FILE - edit the master and reinstall. # (- installed on Wed Mar 11 20:54:12 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) # DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.XXXXs7xGhE installed on Wed Mar 11 20:53:46 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) USER=root JAVA_HOME=/usr/java/latest 59 20 * * * /etc/webmin/cron/tempdelete.pl #/5 * * * * /opt/watchdog/startupWatchdog.sh
Also if ran multiply times it adds more comments. This file could get messy.
Shell Scripting Syntax (Toggle Plain Text)
# DO NOT EDIT THIS FILE - edit the master and reinstall. # (- installed on Wed Mar 11 20:54:31 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) # DO NOT EDIT THIS FILE - edit the master and reinstall. # (- installed on Wed Mar 11 20:54:12 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) # DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.XXXXs7xGhE installed on Wed Mar 11 20:53:46 2009) # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $) USER=root JAVA_HOME=/usr/java/latest 59 20 * * * /etc/webmin/cron/tempdelete.pl #/5 * * * * /opt/watchdog/startupWatchdog.sh
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
Of course it won't work now. You changed the watchdog line in crontab from as you originally specified, to You will have to
The one-line shell command I supplied does work. You just have to match the original conditions you specified.
Remember, Linux does not have a "Do What I Mean" interface; it has only a "Do What I Say" interface. You need to be explicit and correct when you tell Linux to do something. Using less caution can be catastrophic to the running system.
As to the leading comments in the file that replicate every time you edit the crontab, you should file a bug report with your linux distributor. This particular condition is known as 'non-idempotence'. 'Crontab -e' should strip out those leading comments before letting the user edit the file, unless their intent was to maintain a semi-absolute log of each time the crontab was editted.
Shell Scripting Syntax (Toggle Plain Text)
*/5 * * * */opt/watchdog/startwatchdog.sh
Shell Scripting Syntax (Toggle Plain Text)
/5 * * * * /opt/watchdog/startupWatchdog.sh
- Restore the leading asterisk so that cron doesn't complain and so that the crontab is syntactically correct, and
- change the code I provided to handle the other difference, or restore the original file name.
The one-line shell command I supplied does work. You just have to match the original conditions you specified.
Remember, Linux does not have a "Do What I Mean" interface; it has only a "Do What I Say" interface. You need to be explicit and correct when you tell Linux to do something. Using less caution can be catastrophic to the running system.
As to the leading comments in the file that replicate every time you edit the crontab, you should file a bug report with your linux distributor. This particular condition is known as 'non-idempotence'. 'Crontab -e' should strip out those leading comments before letting the user edit the file, unless their intent was to maintain a semi-absolute log of each time the crontab was editted.
•
•
•
•
Of course it won't work now. You changed the watchdog line in crontab fromas you originally specified, toShell Scripting Syntax (Toggle Plain Text)
*/5 * * * */opt/watchdog/startwatchdog.shYou will have toShell Scripting Syntax (Toggle Plain Text)
/5 * * * * /opt/watchdog/startupWatchdog.sh
- Restore the leading asterisk so that cron doesn't complain and so that the crontab is syntactically correct, and
- change the code I provided to handle the other difference, or restore the original file name.
The one-line shell command I supplied does work. You just have to match the original conditions you specified.
Remember, Linux does not have a "Do What I Mean" interface; it has only a "Do What I Say" interface. You need to be explicit and correct when you tell Linux to do something. Using less caution can be catastrophic to the running system.
As to the leading comments in the file that replicate every time you edit the crontab, you should file a bug report with your linux distributor. This particular condition is known as 'non-idempotence'. 'Crontab -e' should strip out those leading comments before letting the user edit the file, unless their intent was to maintain a semi-absolute log of each time the crontab was editted.
Thank you for all the help!
Do you think it would be possible to just run another command to delete the added 3 lines?
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
Thank you for all the help!
Do you think it would be possible to just run another command to delete the added 3 lines?
Shell Scripting Syntax (Toggle Plain Text)
# Comment out the entry crontab -l | \ egrep -m 3 -v "^# DO NOT EDIT THIS FILE|^# \(- installed on|^# \(Cron version --" | \ sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1=' | \ crontab - # Uncomment the entry crontab -l | \ egrep -m 3 -v "^# DO NOT EDIT THIS FILE|^# \(- installed on|^# \(Cron version --" | \ sed -e 's=^#\(.*/opt/watchdog/startwatchdog.sh$\)=\1=' | \ crontab -
This should make the file idempotent: there should now only be the initial three comment lines in the crontab. If you want even those erased, you'll have to modify the crontab program.
![]() |
Similar Threads
- Automated FS usage monitoring (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: ZIP Script modified - Help
- Next Thread: real noob syntax question
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





