Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a shell script in one minute later in linux?

Tags:

bash

How to start a shell script in one minute later?

Suppose there are two bash files a.sh and b.sh
I want to execute b.sh one minute(or several seconds) after a.sh executed.
what should I code in a.sh ?

like image 292
Alex Chan Avatar asked May 08 '12 10:05

Alex Chan


People also ask

How do I run a shell script at a specific time?

From the interactive shell, you can enter the command you want to run at that time. If you want to run multiple commands, press enter after each command and type the command on the new at> prompt. Once you're done entering commands, press Ctrl-D on an empty at> prompt to exit the interactive shell.

How do I run a script every minute in Linux?

How does it work? The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. An asterisk in the every field means run given command/script every minute.

How do I delay a script in Linux?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.

How do you wait 30 seconds in bash?

It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.


2 Answers

Simple. you want to use 'at' to schedule your job. and 'date' to calculate your moment in the future.

Example:

echo b.sh | at now + 1 minute

or:

echo b.sh | at -t `date -v+60S "+%Y%m%d%H%M%S"`

-v+60S adds 60 seconds to current time. You can control exactly how many seconds you want to add.

But usually, when people wants one program to launch a minute after the other, they are not 100% sure it will not take more or less than a minute. that's it. b.sh could be launched before a.sh is finished. or a.sh could have finished 30 seconds earlier than "planned" and b.sh could have started faster.

I would recommend a different model. Where b.sh is launched first. a.sh creates a temp file when it starts. execute is tasks and delete its temp file at the end. b.sh watch for the temp file to be created, then deleted. and start its tasks.

like image 60
Mathieu J. Avatar answered Oct 13 '22 10:10

Mathieu J.


Make the final line of a.sh:

sleep 60 && b.sh

(If b.sh is not in a directory in PATH, make that a full path to b.sh.)

like image 35
William Pursell Avatar answered Oct 13 '22 11:10

William Pursell