0

I have some script running with cron and I need to stop them, so I want to build a shell script that comments out cron jobs containing a certain word.

How can I comment out and uncomment the cron job containing "start_bots" through shell script? I saw some solutions using "sed", but I'm not familiar with it.

*/3 * * * * /home/pi/Development/instabot.py/start_bots.sh
0 */3 * * * /home/pi/Development/instabot.py/stop_bots.sh
0 0 */3 * * /home/pi/Development/instabot.py/remove_logs.sh

1 Answers1

0
sed -i /start_bots/s/^/#/ cronfile
  • The -i option streams the edited content into a new file and then renames it behind the scenes.
  • /start_bots/ matches a line with start_bots
  • s do the substitution on the lines matched above
  • Substitution will insert a comment character # at the beginning of the line ^(^ represent the beginning of the line).
skr
  • 2,146
  • 19
  • 22
  • I get this message "sed: 1: "cronfile": command c expects \ followed by text" running "sed -i /start_bots/s/^/#/ cronfile" on terminal. –  Aug 03 '17 at 11:14
  • what is the question? you are getting error? did you replace `cronfile` with the name of you file. – skr Aug 03 '17 at 12:25
  • 1
    I made it with `crontab -l | sed '/restart_bots/s/^/#/' | crontab -`! –  Aug 03 '17 at 13:36
  • what is the last part of the command `crontab -` ? – skr Aug 03 '17 at 14:39