tags:

views:

39

answers:

3

Excuse me if it is a repeat. I have crontab entries which look like:

*   *    *    *    *   sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh

How to insert a # on the line which contains a call to "as.sh" using sed?
How to uncomment it back?

+1  A: 

You can use:

sed '/\/as.sh/s/^/#/'

which will replace the start-line zero-width marker with a comment character for all lines containing /as.sh, as shown in the following example:

pax> echo '
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
' | sed '/\/as.sh/s/^/#/'

* * * * * sleep 15;/etc/opt/wer.sh
#1 * * * * /opt/sfm/qwe/as.sh

But you need to keep in mind a couple of things.

  • It's usually not enough to just change the file, you also need to notify cron that it needs to re-read it. This is automatic if you use the crontab command itself but you may have to send a signal to cron if you're editing the file directly.
  • It's not always a good idea to turn scripts loose on important system files. Make sure you know what you're doing and don't trust any old codger on the net (including me). Test it thoroughly.

To get rid of the marker, use:

sed '/\/as.sh/s/^#//'

This is the opposite operation, it finds those lines containing /as.sh and substitute any # character at the start of the line with nothing.

paxdiablo
+1  A: 

To add the comment:

sed -e "s/\(.*\)\(as.sh\)/\#\1\2/g"

To remove the comment:

sed -e "s/\(^#\)\(.*\)\(as.sh\)/\2\3/g"
Amardeep
A: 

use crontab -e to modify the current user's crontab.

ghostdog74