943,662 Members | Top Members by Rank

Ad:
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 5th, 2009
0

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

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!


Click to Expand / Collapse  Quote originally posted by Fest3er ...
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!

Reputation Points: 10
Solved Threads: 0
Light Poster
egmik3 is offline Offline
34 posts
since Dec 2006
Mar 5th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
egmik3 is offline Offline
34 posts
since Dec 2006
Mar 11th, 2009
1

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

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 11th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
egmik3 is offline Offline
34 posts
since Dec 2006
Mar 11th, 2009
0

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

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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
May 15th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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?
Reputation Points: 10
Solved Threads: 0
Light Poster
egmik3 is offline Offline
34 posts
since Dec 2006
May 15th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by egmik3 ...
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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
May 3rd, 2010
0
Re: How do I? Edit Crontab using a Script?
This basically runs the commands in sequence: ESC, :, grep for "watchdog" and substitute the beginning of the line with a #, ESC, wq!, RETURN
-Dan
Last edited by danwoz; May 3rd, 2010 at 9:30 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danwoz is offline Offline
1 posts
since May 2010
May 18th, 2010
0
Re: How do I? Edit Crontab using a Script?
Click to Expand / Collapse  Quote originally posted by Fest3er ...
First, it assumes that the editor you are using to edit the crontab is vi(). If you've 'export EDITOR=vi' in the script, then this should be OK.

It is feeding vi() commands to the 'crontab -e' command's standard input. Were you to do this by hand, you would:
Shell Scripting Syntax (Toggle Plain Text)
  1. shell prompt> crontab -e
  2. :g/watchdog/s/^/#/<ENTER>
  3. :wq!<ENTER>
  4. shell prompt>

:g is a global command; it searches for the first instance of watchdog, then executes the command that follows. In this case the following command is a substitution: change the beginning-of-line (^) to # (i.e., prepend a # to the line).

:wq! writes the file out (w) regardless of write privilege (!) and quits (q).
The 'character' between the two commands (^[) looks to be an escape (0x1b, 033, ESC) character. I think this should be a CR (see below).

If you are certain there won't be anything (anyone) else changing the crontab, it's far clearer to make a temp copy of the crontab, then use sed | crontab to change the line (as illustrated in my previous post).

Oh, the ^M is supposed to be a <CTRL-M> (0x0d, 015, CR). It might be clearer if it was
Shell Scripting Syntax (Toggle Plain Text)
  1. (echo -e ^[:g/watchdog/s/^/#/\r:wq!\r)|crontab -e
Vim() might properly interpret the ^M, but I usually don't trust it to; I use the actual ASCII control character by habit.
Yeah, way to go

Toni Vlaic
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Tomaker is offline Offline
4 posts
since May 2010
Jun 9th, 2010
0
Re: How do I? Edit Crontab using a Script?
Will This work on AIX
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fsh68 is offline Offline
1 posts
since Jun 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: How to find size of a file on remote system from present working system
Next Thread in Shell Scripting Forum Timeline: shell scripting last command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC