Merge two crontab entries into a single entry - linux

In my crontab file I have two entries
00 13 * * * DISPLAY=:0.0 /home/noob/.scripts/backup.sh
00 21 * * * DISPLAY=:0.0 /home/noob/.scripts/backup.sh
Now can I write a single entry which executes the backup.sh file # 1:00PM and 9:00PM.

00 13,21 * * * DISPLAY=:0.0 /home/noob/.scripts/backup.sh

Related

cron job - 2 different jobs scheduling

*/5 * * * * /dev/file1/test/test1/fls/mdm/test1.sh
*/7 * * * * /dev/file1/test/test1/fls/mdm/test2.sh
I have 2 cron jobs - job1 and job2. I need to schedule the second job to run few minutes later than ongoing cron job.
Can anyone guide me how to achieve this?
I entered this after typing crontab -e ->
00 05 * * * * /dev/file1/test/test1/fls/mdm/test1.sh 02 05 * * * * /dev/file1/test/test1/fls/mdm/test2.sh
When I try the below one, it is working.
00 */5 * * * /dev/file1/test/test1/fls/mdm/test1.sh
02 */5 * * * /dev/file1/test/test1/fls/mdm/test2.sh
I'm not sure if I use the second script it will be triggered in different times.
Error Message: errors in crontab file can't install
00 05 * * * * /dev/file1/test/test1/fls/mdm/test1.sh
02 05 * * * * /dev/file1/test/test1/fls/mdm/test2.sh
I guess this should do the job as the first will run at 05 00 am and the second will run at 05 02 am.

Need to run a sh everyday at 9 am and keep running every 10 secs

Im programming a raspberry camera
So, I need to execute a sh script everyday at 9 AM but then, the script has to run every 10 seconds.
Like it starts doing photos at 6 am and do them every 10 seconds until i automaticlly reboot the system so it stops the command.
00 09 * * 1 watch -n 10 sh /home/pi/timelapse/process1.sh
00 09 * * 2 watch -n 10 sh /home/pi/timelapse/process1.sh
00 09 * * 3 watch -n 10 sh /home/pi/timelapse/process1.sh
00 09 * * 4 watch -n 10 sh /home/pi/timelapse/process1.sh
00 09 * * 5 watch -n 10 sh /home/pi/timelapse/process1.sh
00 17 * * 1 sudo reboot
00 17 * * 2 sudo reboot
00 17 * * 3 sudo reboot
00 17 * * 4 sudo reboot
00 17 * * 5 sudo reboot
30 17 * * 1 sh /home/pi/timelapse/newimage/video.sh
30 17 * * 2 sh /home/pi/timelapse/newimage/video.sh
30 17 * * 3 sh /home/pi/timelapse/newimage/video.sh
30 17 * * 4 sh /home/pi/timelapse/newimage/video.sh
30 17 * * 5 sh /home/pi/timelapse/newimage/video.sh
reboot crontab have to stop first command at 5 PM so i dont need to program it to stop. Then it does a video with all the photos in the command.
I tried with crontab, watch and sleep but crontab just dont run them even in an sh script.
it is supose to create images in a folder and it doesnt. I let the code right here. when i execute it manually, it works.
raspistill -w 1920 -h 1080 -o /home/pi/timelapse/images/imageBTW.jpg DATE=$(date +"%Y-%m-%d_%H%M%S") for file in "/home/pi/timelapse/images/imageBTW.jpg" ; do convert "$file" \ -pointsize 72 -fill white -annotate +100+100 \ %[exif:DateTimeOriginal] /home/pi/timelapse/newimage/$DATE.jpg done rm /home/pi/timelapse/images/imageBTW.jpg
Try reading this: man 5 crontab
There you will find the information, that cron works only with minute precision.
But you can write the following into your crontab:
0 9 * * 1-5 watch -n 10 sh -c 'while /home/pi/timelapse/process1.sh; do sleep 10; done'
BTW whom do you want to spy out?

Why sed is duplicating the first added line (sending the sed output to the same file)?

Starting file (/etc/crontabs/root):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
I want to replicate this file, remove comments... and adding the content to the same file, so I run:
sed '/^#/ d; s/\/etc/\/root\/etc/' /etc/crontabs/root >> /etc/crontabs/root
... which should remove all comments, replace /etc with /etc/root... and add the result to the same file.
Wrong output (please note the "extra" line starting with */15):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
*/15 * * * * run-parts /root/etc/periodic/15min
0 * * * * run-parts /root/etc/periodic/hourly
0 2 * * * run-parts /root/etc/periodic/daily
0 3 * * 6 run-parts /root/etc/periodic/weekly
0 5 1 * * run-parts /root/etc/periodic/monthly
*/15 * * * * run-parts /root/root/etc/periodic/15min
Expected/wanted output:
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
*/15 * * * * run-parts /root/etc/periodic/15min
0 * * * * run-parts /root/etc/periodic/hourly
0 2 * * * run-parts /root/etc/periodic/daily
0 3 * * 6 run-parts /root/etc/periodic/weekly
0 5 1 * * run-parts /root/etc/periodic/monthly
Ideas?
You shouldn't write out to the same file that you're reading from. It will read the lines that you added to the file, and continue processing them. So when it gets to the lines with /root/etc, it will replace the /etc in that, producing /root/root/etc. It could conceivably get stuck in an infinite loop, since it keeps extending the file and will never reach the end.
What you can do is copy the crontab file to a new file, then use sed to append to that.
cp /etc/crontabs/root /tmp/new_crontab
sed '/^#/ d; s/\/etc/\/root\/etc/' /etc/crontabs/root >> /tmp/new_crontab
cp /tmp/new_crontab /etc/crontabs/root
If you want a one liner, it's fairly straightfoward using ed:
printf "%s\n" ka '1,$t' "'a,\$g/^#/d" "'a+1,\$s/\/etc/\/root\/etc/" w | ed -s /etc/crontabs/root
or a bit easier to read using a heredoc:
ed -s /etc/crontabs/root <<'EOF'
ka
1,$t
'a,$g/^#/d
'a+1,$s/\/etc/\/root\/etc/
w
EOF

Add a job to cron and execute every 1h and x mins

I would like to execute a cronjob every 1 hour and x mins
I tried adding to crontab:
*/53 */1 * * * /myscrop.sh
But the scrip is executed every 53 mins and not 1h and 53 mins
With "regular" crontab entries, this is rather difficult to do without a large number of them.
Since you want your job to happen every 1h:53m (113m), you may just be able to run every minute but with a suitable step value, something like:
*/113 * * * command-to-run
although I've never tested this with large skip values like that.
If that works, it's probably the easiest solution.
If you're running in an environment where that doesn't work, you can revert to the trick of actually running the script every minute but having it decide whether or not it does the actual "payload" of the task.
To do this, use a state file to record the last time the payload was run. Though the script itself runs very often, the payload only runs when enough time since the last has passed.
For example, the following script only runs the payload (the final echo) every seven seconds, regardless of how often the script is called:
#!/bin/bash
# Configuration items.
stateFile=/tmp/lastPayload # File holding time of last payload run.
minGap=7 # How often payload should run (seconds).
# Get last payload time (checks for non-valid data).
((lastTime = 0))
[[ -f ${stateFile} ]] && lastTime="$(cat ${stateFile})"
[[ "${lastTime}" =~ [1-9][0-9]* ]] || ((lastTime = 0))
# Exit if not enough time since last payload run.
thisTime=$(date +%s)
((timeGap = thisTime - lastTime))
[[ ${timeGap} -lt ${minGap} ]] && exit
# Update last payload run time and execute payload.
echo ${thisTime} >${stateFile}
echo "[${lastTime}] [${thisTime}] [${timeGap}]"
If that script is called test_periodic.sh, you can test it with:
while true ; do ./test_periodic.sh ; sleep 1 ; done
This will run it every second but you'll notice the payload is only done every seven seconds (other than the first time), as expected:
[0] [1504041523] [1504041523]
[1504041523] [1504041530] [7]
[1504041530] [1504041537] [7]
[1504041537] [1504041544] [7]
For 1h:53m, set minGap to be 6780 (113 minutes, in seconds) and have cron run the script every minute.
Cron doesn't add the hours and minutes for the interval. */53 */1 * * * runs at 1:53, 2:53, 3:53, etc.
You'd have to create separate schedules. For a 53 minute interval, the set would look as follows:
0 0 * * * /myscrop.sh
53 0 * * * /myscrop.sh
46 1 * * * /myscrop.sh
39 2 * * * /myscrop.sh
32 3 * * * /myscrop.sh
25 4 * * * /myscrop.sh
18 5 * * * /myscrop.sh
11 6 * * * /myscrop.sh
4 7 * * * /myscrop.sh
57 7 * * * /myscrop.sh
50 8 * * * /myscrop.sh
43 9 * * * /myscrop.sh
36 10 * * * /myscrop.sh
29 11 * * * /myscrop.sh
22 12 * * * /myscrop.sh
15 13 * * * /myscrop.sh
8 14 * * * /myscrop.sh
1 15 * * * /myscrop.sh
54 15 * * * /myscrop.sh
47 16 * * * /myscrop.sh
40 17 * * * /myscrop.sh
33 18 * * * /myscrop.sh
26 19 * * * /myscrop.sh
19 20 * * * /myscrop.sh
12 21 * * * /myscrop.sh
5 22 * * * /myscrop.sh
58 22 * * * /myscrop.sh
51 23 * * * /myscrop.sh
And note that the interval always restarts at the beginning of the day.

Crob job excluding some hours

Currently I'm having two cronjobs as following,
0 */6 * * * root job1
0 */2 * * * root job2
I need to avoid running the job2 when job1 is running.
Is there a way to exclude the time (00 00 , 06 00 , etc . . ) from the job2 cron.
Thanks.
No, there's no exclusion syntax. Change job2 to:
0 2,4,8,10,14,16,20,22 * * * root job2

Resources