jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Alter cron jobs with bash script

Sat Jan 06, 2018 11:31 am

Hello,

Is it possible to delete an old cron job and insert a new cron job using a bash script?

thanks
Jon

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 11:48 am

how is 'the old cronjob' identified (in terms of a machine)?

normally you alter the crontab by the 'crontab' command (if you cease from editing the crontab directly by means of any text editor)

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:20 pm

Found it:

To Add:
crontab -u pi -l ; echo "30 4 * * * /home/pi/Desktop/randomtestfile.txt"

To remove:
crontab -u pi -l | grep -v "30 4 * * * /home/pi/Desktop/randomtestfile.txt"
Last edited by jondallimore on Sat Jan 06, 2018 12:37 pm, edited 1 time in total.

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:32 pm

Try this:

Code: Select all

#!/bin/bash

#The new cron job
CRON_ADD="1 * * * * /path/to/cron/command"

#Get crontab and remove lines containing "something" with sed
CRON=$(crontab -l | sed -n "/something/!p")

#Append the new job
CRON="$CRON\n$CRON_ADD\n"

#Modify the crontab
printf "$CRON" | crontab -
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:46 pm

tried both possibilities now - neither working. will bash my head against this brick wall a bit more later, hungry now.

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:47 pm

Please post the script that does not work, the script I posted works for me..
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:52 pm

Ive copied and pasted your script word for word. When I go into crontab through the terminal after running it theres nothing there.

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 12:59 pm

If you are running the script as root, you must add "-u pi" after each "crontab" command or else you will modify the crontab for "root".

Code: Select all

#!/bin/bash

#The new cron job
CRON_ADD="1 * * * * /path/to/cron/command"

#Get crontab and remove lines containing "something" with sed
CRON=$(crontab -u pi -l | sed -n "/something/!p")

#Make sure crontab ends with newline
if [ $(printf "$CRON" | tail -c 1 | wc -l) -eq 0 ]; then
  CRON="$CRON\n"
fi

#Modify the crontab
printf "$CRON$CRON_ADD\n" | crontab -u pi -
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 1:12 pm

first time round, I checked the pi and the root crontabs - nothing in either.

the new one works for the pi crontab, and if I want to modify the root crontab, just change the username to root.

thanks

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 1:25 pm

Great! Please note that I edited the last post to use "printf" rather that "echo" in the if..then..fi block
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 7:32 pm

Ended up using the code below so that I could delete cron jobs containing specific text, and add in multiple jobs:

Code: Select all

#!/bin/bash

crontab -l -u pi | grep -v "Monday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Tuesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Wednesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Thursday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Friday 2" | crontab -u pi -

(
crontab -l -u pi
cat <<'EOF'
#ADD CRON JOBS HERE#
1 1 * * * test
2 2 * * * test
3 3 * * * test
EOF
) | crontab -u pi -

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 7:57 pm

jondallimore wrote:
Sat Jan 06, 2018 7:32 pm
Ended up using the code below so that I could delete cron jobs containing specific text, and add in multiple jobs:

Code: Select all

#!/bin/bash

crontab -l -u pi | grep -v "Monday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Tuesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Wednesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Thursday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Friday 2" | crontab -u pi -

(
crontab -l -u pi
cat <<'EOF'
#ADD CRON JOBS HERE#
1 1 * * * test
2 2 * * * test
3 3 * * * test
EOF
) | crontab -u pi -
You will modify the crontab 6 times using this method but if you are fine with that, go for it! :)
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 8:04 pm

yep - I have a couple of jobs I dont want removing every time. removing them by finding specific jobs is neater, even if it takes a few more lines

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 8:11 pm

jondallimore wrote:
Sat Jan 06, 2018 8:04 pm
yep - I have a couple of jobs I dont want removing every time. removing them by finding specific jobs is neater, even if it takes a few more lines
What I meant was that you will actually save and use a new crontab 6 times during that script. You could load the crontab once, modify it and save it again one time which would be the nicest approach.

Code: Select all

#!/bin/bash

#The new cron job
CRON_ADD="1 * * * * /path/to/cron/command"

#Get crontab
CRON=$(crontab -u pi -l)

#Remove lines with "monday" and "tuesday"
CRON=$(printf "$CRON" | sed -n "/monday/!p")
CRON=$(printf "$CRON" | sed -n "/tuesday/!p")

#Make sure crontab ends with newline
if [ $(printf "$CRON" | tail -c 1 | wc -l) -eq 0 ]; then
  CRON="$CRON\n"
fi

#Store the new crontab
printf "$CRON$CRON_ADD\n" | crontab -u pi -
Please do not ask questions in private messages, they will not help others.

jondallimore
Posts: 215
Joined: Thu Aug 04, 2016 11:02 pm

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 8:12 pm

it works. Im going to leave it as is now.

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Alter cron jobs with bash script

Sat Jan 06, 2018 8:48 pm

Nice! But the lines:

Code: Select all

crontab -l -u pi | grep -v "Monday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Tuesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Wednesday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Thursday 2" | crontab -u pi -
crontab -l -u pi | grep -v "Friday 2" | crontab -u pi -
should really be modified to:

Code: Select all

CRON=$(crontab -l -u pi)
CRON=$(printf "$CRON" | grep -v "Monday 2")
CRON=$(printf "$CRON" | grep -v "Tuesday 2")
CRON=$(printf "$CRON" | grep -v "Wednesday 2")
CRON=$(printf "$CRON" | grep -v "Thursday 2")
CRON=$(printf "$CRON" | grep -v "Friday 2")
printf "$CRON" | crontab -u pi -
Since this would only save and effect a new crontab once instead of 5 times.
Please do not ask questions in private messages, they will not help others.

Return to “General discussion”