linux script to run multiple commands at specific time - linux

I need some help in writing a Linux script that do the following:
command 1
command 2
wait 10 minutes
command 3
command 4
and this script should run automatically at specific time for example 4 am...
Thank in advance

You can create a script.sh like:
#!/bin/bash
command 1
command 2
sleep 600 # 600 seconds = 10 min
command 3
command 4
And then create a cronjob:
0 4 * * * /bin/bash /path/to/script.sh
You can see more info of cron in https://stackoverflow.com/tags/cron/info

if you want the job to run once at a future time, instead of cron use at
at 4am tomorrow <<END
command 1
command 2
sleep 600
command 3
command 4
END
One of the advantages of at is that it will execute the commands using your current environment. The limited environment provided by cron is a cause of confusion for many people.

Related

How to Solve expecting EOF in Cron Job

I have a server running Linux operating system. I am trying to schedule a cron job, in crontab file, to run a task every two weeks (Fortnight) on Tuesday at 9 am. I tried to run the following command:
0 9 * * 2 root test $((10#$(date +\%V)\%2)) -eq 0 && ( java -jar /email/emailRemind.jar )
This script does not work, it shows this message
/bin/sh: 1: arithmetic expression: expecting EOF: 10#24%2"
Any thoughts?

How to configure cron to run at multiple random intervals?

I'm working on a new project and I would like to setup a cron to run every 6-8 hours at a random minute. Any suggestions on the best way to achieve this would be greatly appreciated.
Let's run the cron every 6 hours:
0 */6 * * * /path/to/script.sh
Now, in your bash script:
#!/bin/bash
maxdelay=$((2*60)) # 2 hours converted to minutes
delay=$(($RANDOM%maxdelay)) # a random delay
(sleep $((delay*60)); /path/to/script.sh) & # background a subshell to wait, then run the script
You can also use anacron for RANDOM_DELAY feature.

Execute a shell script everyday at specific time [duplicate]

This question already has answers here:
How to write a cron that will run a script every day at midnight?
(6 answers)
Closed 7 years ago.
I have a simple shell script that just checks the contents of a directory and if anything was added during the day makes a copy of it to a backup folder.
I'd like to execute this script at the end of each day (let's assume at 23:55).
The system(Debian) which this scripts reside on it, is always on (kind of server)
How can I do that?
To add a crontab job, type the following command at a UNIX/Linux shell prompt:
$ sudo crontab -e
Add the following line:
1 2 3 4 5 /path/to/script
where
1: Minutes (0-59)
2: Hours (0-23)
3: Days (1-31)
4: Month (1-12)
5: Day of the week(1-7)
/path/to/script - your own shell script
In your case it would be:
55 23 * * * /path/to/yourShellScript
You want to edit your crontab file using
crontab -e
Then you want to add
55 23 * * * COMMAND TO BE EXECUTED
for more info look at this
I'm anything, but a linux expert, but a quick Google search conjured up this:
watch -n <your time> <your command/script>
This should do the trick. For more information, check this out: http://www.linfo.org/watch.html
sudo crontab -e
55 23 * * * some_shell_script.sh
Check out the Cron task scheduler built in to Debian. Simply add an entry for your script to your crontab file (see: https://help.ubuntu.com/community/CronHowto).

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Setting cron job end time

I want to set up a cron job which will execute a command every hour. However, I want that this command should be started at 10 A.M and should run every hour till 4 P.M. This job is to run daily between these times. The command is nothing but a call to a Perl script. Following crontab entry runs fine and invokes the script every hour
* */1 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
Is there a way to limit the timings of this cron job so that it runs only between 10 A.M and 4 P.M ?
man 5 crontab is your friend. (Your example does not do what you claim it does; /1 is the default skip and therefore redundant, and that spec therefore runs once per minute due to the leading * instead of 0.)
0 10-15 * * * your command here
(I used 15, because it occurs to me that "between 10 and 4" is an exclusive range so you don't want to run at 16:00.)
If you want the script to be run every hour you can do something like this:
[code]
00 10,11,12,13,14,15,16 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
[/code]
This means when the minutes hit 00 and the hour hits any of 10 11 12 13 14 15 16 the script will be run
In your Perl script (or in a wrapper for the Perl script), you can use localtime to check the hour and exit if it isn't between 10am and 4pm:
use strict;
use warnings;
my #lt=localtime;
my $hour=$lt[2];
unless($hour>=10 and $hour<=16)
{
print "Not between 10am and 4pm. Exiting.\n";
exit;
}
#put the rest of your code here.

Resources