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

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?

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.

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.

Set Cronjob to Run Every 5 Minutes From 9:30am to 4:00pm

I need to set a cronjob to run a bash script every 5 minutes, starting at 9:30am until 4:00pm.
I have the following but, it's not quite right...
Cronjob:
*/5 9-16 * * * /path/to/directory/job.sh > /path/to/log/file/job.log 2>&1
What you have there is a line that will run the command every five minutes between 09:00 and 16:55 (all ranges here are inclusive).
What you're trying to achieve can be done relatively simply with three separate crontab lines:
30-59/5 9 * * * /path/to/directory/job.sh > /path/to/log/file/job.log 2>&1
*/5 10-15 * * * /path/to/directory/job.sh > /path/to/log/file/job.log 2>&1
0 16 * * * /path/to/directory/job.sh > /path/to/log/file/job.log 2>&1
The first handles the case between 09:30 and 09:55, the second every five minutes between 10:00 and 15:55, and the final one the single job at 16:00.
Cron doesn't have a syntax for expressing that directly, so you'll need 3 separate lines: one for 9:30-9:55, one for 10:00-15:55, and one for 16:00.
I think this is correct:
30-55/5 9 * * * <command>
*/5 10-15 * * * <command>
0 16 * * * <command>

How to convert timestamp to hour/minutes/seconds in shell script?

I am calculating how much time my code is taking in shell script -
date1=$(date +"%s")
# some code here
date2=$(date +"%s")
diff=$(($date2-$date1))
echo "Time Taken - $(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed."
Above script prints out time taken in minutes and seconds. How can I add it for hours as well? Meaning it should print out Time Taken - 0 hours 54 minutes 0 seconds something like this.
Try this:
echo "Time Taken - $((diff /60/60)) hours and $(($diff % 60)) minutes and $(($diff % 60)) seconds elapsed."
if you're starting with integer seconds that all fall within a single day, and only need HH:MM:SS, here's a very strange way to use jot + bc :
jot -w 'obase = 60; ' - 91 86400 9091 | bc
01 31
02 33 02
05 04 33
07 36 04
10 07 35
12 39 06
15 10 37
17 42 08
20 13 39
22 45 10

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