How to automate execution of commands by day and hour on Debian? - bash

So I am a working on a simple home lightning control using Raspberry Pi (and raspbian). I can turn or dim lights by writing commands to a zigbee dongle (through a serial interface) by running a command like:
sudo echo "#1*##*1231#*9#" > /dev/ttyUSB0
One of its main functions is to program "scenes" so you can turn lights on and off at certain hour or day.
So how can I automate a bash command to run lets say monday, tuesday and saturday at 8:55AM every week? Thanks!

Use crontab. Those threads will help you:
how to set cronjob for 2 days?
Crontab Day of the Week syntax
So:
crontab -e # edit crontab file
and then insert
55 8 * * 1,2,6 /usr/local/bin/my_cool_script
In case crontab is not flexible enough for your needs, you can use Ruby Rufus scheduler

Related

Resource utilisation of sleep

The problem I want to tackle is as follows. I have a long(1 to 2 hours) running task that has to be run everyday. So the goto option was cron. But the catch is that I have to give a 24 hour gap between successive runs. So using cron now would involve rewriting the cron job file after every run. This might be clear after this example.
The long running job 'LR' starts at 6PM on Monday and finishes at 7:30PM sameday.
On Tuesday it's supposed to start at 7:30 PM and not 6PM (like it did on monday). This is because there has to be a 24hr gap between successive runs.
The obvious option here was to have a process running an infinite loop. start the LR job. Then sleep for 24hr and continue with the loop. This works perfectly too. In my setup there is a bash script which is running this loop.
while [ 1 == 1 ]; do
/bin/jobs/long_run.py
/bin/jobs/cleanup.sh
sleep 86400
done
So my question is what is the total amount of CPU resource spent and what is the RAM usage.
Not sure if this affects the answer in anyway; I'm running this on termux on an android phone.
Also please recommend other light weight options.
There is nothing to worry about resources, while a script executes sleep, it really sleeps. You should worry for if anything happens between two executions, like restart, downtime etc. This structure:
while true; do
sh script.sh
sleep 86400
done
does not resume and you don't save the time for the next execution anywhere. Similar to this structure is to have a wrapper, suppose f() is your jobs
f() {
echo working
}
wrapper() {
f
echo sleeping
sleep 86400
wrapper
}
wrapper
so now you call the wrapper, which works, sleeps and calls itself. You could use just this, if you are ok with what could go wrong, at least print the datetime somewhere.
You can replace the internal sleep and wrapper call with job scheduling with cron or at. Probably at is not a standard packet for all distributions (at least not for mine) while cron is. You could install it. For at the wrapper would be like this:
wrapper() {
f
at now +1 day wrapper
}
With cron, you could edit the crontab, like this but better use a crontab file like this, what you have to do is to parse date command, create the date prefix, update crontab.
Note: There may be other cron jobs for user, existing or added after that, this is considered in the last link.

Crontab task scheduled every hour stops running from 11am to 12am

I have a rather weird issue. My aim is to use ffmpeg to grab a screenshot from a home CCTV cameras rtsp stream every hour. I want to do this in order to make a timelapse. However everyday from 11am to 12am (the next day) there are no snapshots saved.
On an always on Debian machine, this is the shell script I have that crontab calls:
dt=$(date +"%d%m%2y%I%M%S")
ffmpeg -rtsp_transport tcp -i "rtsp://IP:554/..." -frames 1 /user/snapshots/ch1/$dt.jpg
Running it by itself works fine and saves a jpg snapshot successfully to the right folders.
In crontab -e I have the following line:
0 * * * * /bin/sh //user/snap.sh
Thanks.
%I is the hour on a 12-hour clock (intended to be used with %p), so your afternoon files are overwriting the morning ones. Use %H instead.
You should add something like
0 11-0 * * * /bin/sh //user/snap.sh
Mean task will start every minute 0 from every hour from 11AM to 12AM

Scheduling a script

I want to schedule a script at different timings on different day but with a single crontab entry. e.g I want to schedule a script like below:
it should run at
30 8-5 * * 1-4 script.sh on mon-thu
and same should run at:
30 11-5 * * 5-6 script.sh on Fri,Sat.
Please suggest how to schedule this in one crontab entry.
You can't.
Just being curious: Is there any advantage in having it into one line, except that it saves you typing "script.sh" twice?

Running a shell script once a day at random time [duplicate]

This question already has answers here:
Cron jobs and random times, within given hours
(13 answers)
Closed 9 years ago.
Need run a shell script once a day at random time. (so once every day between 00:00-23:59).
I know the sleep command, and the cron too, but
the cron has not random times
and the sleep solution - not very nice - my idea is launch the script every midnight and sleep random time at the start of the script.
Is here something more elegant?
If you have the at command, you can combinte the cron and the at.
Run from a cron every midnight the next script:
#!/bin/bash
script="/tmp/script.sh" #insert the path to your script here
min=$(( 24 * 60 ))
rmin=$(( $RANDOM % $min ))
at -f "$script" now+${rmin}min
The above will run the at command every midnight and will execute your script at random time . You should check your crontab how often is the atrun command started. (The atrun runs the commands stored with the at)
The main benefit in comparison with the sleep method: this "survives" the system reboot.
I would simply launch you script at midnight, and sleep for a random time between 0 and 86400 seconds. Since my bash's $RANDOM returns a number between 0 and 32767:
sleep $(( ($RANDOM % 1440)*60 + ($RANDOM % 60) ))
The best alternative to cron is probably at
See at man page
Usually, at reads commands from standard input, but you can give a file of jobs with -f.
Time wise, you can specify many formats. Maybe in your case the most convenient would be
at -f jobs now + xxx minutes
where your scripts gives xxx as a random value from 1 to 1440 (1440 minutes in a day), and jobs contains the commands you want to be executed.
Nothing prevents you from running sed to patch your crontab as the last thing your program does and just changing the next start time. I wouldn't sleep well though.
You can use cron to launch bash script, which generates pseudorandom timestamp and gives it to unix program at
I see you are familiar with bash and cron enough, so at will be a piece of cake for you. Documentation as always "man at" or you can try wiki
http://en.wikipedia.org/wiki/At_(Unix)

Automatic Overnight Execution of R Scripts on Mac

I want to automate overnight execution of some R scripts in order to have the latest data when I start working. Can you guys tell me how to do that? Via AppleScript or the Automator? or other tools?
Ideally, I would get a sample code looking something like this (AppleScript)
tell R.app
execute example.r
at 2:00 every day
Thx for your help
Andreas
You can use cron combined with Rscript to do this.
First create an R script:
#! /usr/bin/env Rscript
print("Hello World!")
And save it as print_hw.R. Then enter at the terminal:
crontab -e
and choose a time to run the script:
0 0 * * * print_hw.R
This runs the script every night at twelve o'clock. This all is under the assumption that print_hw.R can be executed (use chmod) and the script is in your PATH.

Resources