Alex Chan
Alex Chan

Reputation: 1032

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

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 ?

Upvotes: 12

Views: 32214

Answers (7)

Mathieu J.
Mathieu J.

Reputation: 2125

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.

Upvotes: 30

ankit tyagi
ankit tyagi

Reputation: 846

Schedule both the scripts to run at the same time in cron and put the required delay in b.sh.

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360445

If you want to execute the second script some number of seconds after the start of the first script, you can do this in the first:

b.sh &

and this in the second:

sleep 10
# more commands

You could pass the number of seconds as an argument from the first to the second.

Unfortunately, at doesn't do time increments finer than one minute.

Upvotes: 0

iWantSimpleLife
iWantSimpleLife

Reputation: 1954

Use the at command.

See man at for how to use it.

Upvotes: 1

David K
David K

Reputation: 690

You could use the command to sleep your script for 1 minute.

sleep 1m

Then when you wish to call the 2nd script

bash a.sh

Upvotes: 0

larsks
larsks

Reputation: 312332

You can just sleep:

a.sh
sleep 60
b.sh

Or for more complicated cases you can use the at command:

echo b.sh | at now + 1 minute

See the at man page for more information.

Upvotes: 3

William Pursell
William Pursell

Reputation: 212504

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.)

Upvotes: 4

Related Questions