Scheduling a script - shell

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?

Related

How to create cron job to run shell script every minute

I wan to create cron job that runs a shell script every minute I've tried editing contrab -e with:
1 * * * * sh ~/test.sh
to no avail
I recommend using: https://crontab.guru/
This is a really insightful way to create and understand cron jobs.
This should trigger every minute. However, when a cron runs it can run as a different user and that script location might be different. Adding a log file might help you track down what is going on.
* * * * * /bin/sh /home/user/test.sh >> /var/log/myjob.log 2>&1

Cron tasks are not always executed

I have the following cron tasks:
/etc/cron.d/mongo
/etc/cron.d/elastic
These cron jobs only executes scripts located here:
/etc/script/mongo
/etc/script/elastic
These tasks execute every 30 minuts, that's the following cron format:
0,30 * * * *
I don't know why but these tasks aren't executing all times. In 2 hours, for example, they only execute 2-3 times and not 4. These tasks are performing backups, I need to be sure that they execute every 30 minuts.
Why this is happening?
PD: If i execute crontab -e the file is empty, this may cause any problem?

shell script as cron job

Several people have had this problem, but none of the fixes mentioned around the net have worked for me.
I have a simple shell script that copies log files from a few directories, and puts them in one single directory on a remote machine. The script works fine.
I then create a cronjob, which doesnt seem to execute. My crontab looks like:
1 * * * * /bin/bash /home/user/myscripts/getlogs.sh > /tmp/cronscript.out 2>&1
/var/log/cron shows this every minute
Aug 20 17:02:01 hostname crond[2614]: (root) RELOAD (/var/spool/cron/root)
so it cron job executes, but it doesnt seem to run the shell script.

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

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

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)

Resources