cron job - 2 different jobs scheduling - cron

*/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.

Related

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?

Run a cron job every second minute of every hour

I have a cron job that i want to run every second minute of every hour.before i would just run it every minute like
* * * * * /var/www/html/cron.php
but i now need it to run every second minute of ever hour. How can this be done?.
If your operating system is FreeBSD you could use the #every_second for example:
#every_second /var/www/html/cron.php
For other systems, this could work:
Somethinig every hour:
#hourly /var/www/html/cron.php
Or every 45 minutes:
*/45 * * * * /var/www/html/cron.php
From man 5 crontab:
string meaning
------ -------
#reboot Run once, at startup of cron.
#yearly Run once a year, "0 0 1 1 *".
#annually (same as #yearly)
#monthly Run once a month, "0 0 1 * *".
#weekly Run once a week, "0 0 * * 0".
#daily Run once a day, "0 0 * * *".
#midnight (same as #daily)
#hourly Run once an hour, "0 * * * *".
#every_minute Run once a minute, "*/1 * * * *".
#every_second Run once a second.
Also, check this a reference: https://crontab.guru/
In case the system you are using doesn't support the #every_second you could give a try to something like:
* * * * * /var/www/html/cron.php
* * * * * (sleep 30; /var/www/html/cron.php)
Basically, they run at the same time (every minute) but one will wait for 30 seconds before starting, so /var/www/html/cron.php will be called every 30 seconds.
The format for crontab is as follow
m h dom mon dow command
So you can do
0 * * * yourcommand
It will run every hour at 00 minutes
For me, the only solution I could get working is:
2-59/* * * * *
Which translates to simple English as:
At every 60th minute from 2 through 59.
I barely know about cron syntax, so not sure if this is the only way or is at least one of the better ways.

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

Merge two crontab entries into a single entry

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

Resources