How do I? Edit Crontab using a Script?

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 29
Reputation: egmik3 is an unknown quantity at this point 
Solved Threads: 0
egmik3's Avatar
egmik3 egmik3 is offline Offline
Light Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #11
Mar 5th, 2009
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!


Originally Posted by Fest3er View Post
Shell Scripting Syntax (Toggle Plain Text)
  1. shell prompt> crontab -e
  2. :g/watchdog/s/^/#/<ENTER>
  3. :wq!<ENTER>
  4. shell prompt>

Thank you for this!!! This explains a lot!


Shell Scripting Syntax (Toggle Plain Text)
  1. (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)
  1. bash: !\r: event not found

Also when I run it within the script I get this:

Shell Scripting Syntax (Toggle Plain Text)
  1. ./mysql_upgrade: line 20: syntax error near unexpected token `echo'
  2. ./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!

Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 29
Reputation: egmik3 is an unknown quantity at this point 
Solved Threads: 0
egmik3's Avatar
egmik3 egmik3 is offline Offline
Light Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #12
Mar 5th, 2009
Originally Posted by Fest3er View Post
Shell Scripting Syntax (Toggle Plain Text)
  1. crontab -l >/tmp/crontab.a
  2. sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1' /tmp/crontab.a | crontab
  3. 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)
  1. # ./mysql_upgrade
  2. sed: -e expression #1, char 44: unterminated `s' command
  3. crontab: usage error: file name must be specified for replace
  4. usage: crontab [-u user] file
  5. crontab [-u user] [ -e | -l | -r ]
  6. (default operation is replace, per 1003.2)
  7. -e (edit user's crontab)
  8. -l (list user's crontab)
  9. -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)
  1. crontab -l >/tmp/crontab.a

It adds 3 extra lines to the top of crontab.a

Shell Scripting Syntax (Toggle Plain Text)
  1. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  2. # (/tmp/crontab.XXXXYMQNRs installed on Thu Mar 5 16:10:14 2009)
  3. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  4. USER=root
  5. JAVA_HOME=/usr/java/latest
  6.  
  7. 0 9 * * * /etc/webmin/cron/tempdelete.pl
  8. */5 * * * * /opt/watchdog/startupWatchdog.sh
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How do I? Edit Crontab using a Script?

 
1
  #13
Mar 11th, 2009
Originally Posted by Fest3er View Post
The following should do the trick quite easily.

To comment out the line:
Shell Scripting Syntax (Toggle Plain Text)
  1. crontab -l |sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1=' | crontab -

To uncomment the line:
Shell Scripting Syntax (Toggle Plain Text)
  1. crontab -l | sed -e 's=^#\(.*/opt/watchdog/startwatchdog.sh$\)=\1=' | crontab -
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 29
Reputation: egmik3 is an unknown quantity at this point 
Solved Threads: 0
egmik3's Avatar
egmik3 egmik3 is offline Offline
Light Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #14
Mar 11th, 2009
Originally Posted by Fest3er View Post
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)
  1. USER=root
  2. JAVA_HOME=/usr/java/latest
  3.  
  4. 59 20 * * * /etc/webmin/cron/tempdelete.pl
  5. #/5 * * * * /opt/watchdog/startupWatchdog.sh

Heres crontab after "uncomment" code:

Shell Scripting Syntax (Toggle Plain Text)
  1. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  2. # (- installed on Wed Mar 11 20:54:12 2009)
  3. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  4. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  5. # (/tmp/crontab.XXXXs7xGhE installed on Wed Mar 11 20:53:46 2009)
  6. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  7. USER=root
  8. JAVA_HOME=/usr/java/latest
  9.  
  10. 59 20 * * * /etc/webmin/cron/tempdelete.pl
  11. #/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)
  1. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  2. # (- installed on Wed Mar 11 20:54:31 2009)
  3. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  4. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  5. # (- installed on Wed Mar 11 20:54:12 2009)
  6. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  7. # DO NOT EDIT THIS FILE - edit the master and reinstall.
  8. # (/tmp/crontab.XXXXs7xGhE installed on Wed Mar 11 20:53:46 2009)
  9. # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
  10. USER=root
  11. JAVA_HOME=/usr/java/latest
  12.  
  13. 59 20 * * * /etc/webmin/cron/tempdelete.pl
  14. #/5 * * * * /opt/watchdog/startupWatchdog.sh
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #15
Mar 11th, 2009
Of course it won't work now. You changed the watchdog line in crontab from
Shell Scripting Syntax (Toggle Plain Text)
  1. */5 * * * */opt/watchdog/startwatchdog.sh
as you originally specified, to
Shell Scripting Syntax (Toggle Plain Text)
  1. /5 * * * * /opt/watchdog/startupWatchdog.sh
You will have to
  1. Restore the leading asterisk so that cron doesn't complain and so that the crontab is syntactically correct, and
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 29
Reputation: egmik3 is an unknown quantity at this point 
Solved Threads: 0
egmik3's Avatar
egmik3 egmik3 is offline Offline
Light Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #16
May 15th, 2009
Originally Posted by Fest3er View Post
Of course it won't work now. You changed the watchdog line in crontab from
Shell Scripting Syntax (Toggle Plain Text)
  1. */5 * * * */opt/watchdog/startwatchdog.sh
as you originally specified, to
Shell Scripting Syntax (Toggle Plain Text)
  1. /5 * * * * /opt/watchdog/startupWatchdog.sh
You will have to
  1. Restore the leading asterisk so that cron doesn't complain and so that the crontab is syntactically correct, and
  2. 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How do I? Edit Crontab using a Script?

 
0
  #17
May 15th, 2009
Originally Posted by egmik3 View Post
Thank you for all the help!

Do you think it would be possible to just run another command to delete the added 3 lines?
This may be starting to get ugle and specific to Linux or Posix version of certain tools. The following seem to work:
Shell Scripting Syntax (Toggle Plain Text)
  1. # Comment out the entry
  2. crontab -l | \
  3. egrep -m 3 -v "^# DO NOT EDIT THIS FILE|^# \(- installed on|^# \(Cron version --" | \
  4. sed -e 's=\(^.*/opt/watchdog/startwatchdog.sh$\)=#\1=' | \
  5. crontab -
  6.  
  7. # Uncomment the entry
  8. crontab -l | \
  9. egrep -m 3 -v "^# DO NOT EDIT THIS FILE|^# \(- installed on|^# \(Cron version --" | \
  10. sed -e 's=^#\(.*/opt/watchdog/startwatchdog.sh$\)=\1=' | \
  11. 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC