2

I am new to shell scripting, and would like to know how to call the second script 5 seconds after running the first script. In the example below, I would like ./csv3xlsx_osx to run 5 seconds after the python script completes.

python script.py
./csv2xlsx_osx -infile input.csv -outfile ouptut.xlsx

How would one go about doing the equivalent of Javascript's setTimeout() in shell?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 1
    You should use the command `sleep 5` – mathB Oct 20 '17 at 17:21
  • 1
    Five seconds after *starting* the first script? Five minutes after it exits? Do you want to force the first script to exit when the timeout hits, or do you want both to be running concurrently? – Charles Duffy Oct 20 '17 at 17:21
  • Five seconds after it exits – iskandarblue Oct 20 '17 at 17:22
  • Oh -- in *that* case, the comment by mathB is correct. – Charles Duffy Oct 20 '17 at 17:23
  • Where should `sleep 5` be placed ? – iskandarblue Oct 20 '17 at 17:23
  • In the place where you want to actually put the delay, ie. between the two commands. Shell scripting is all synchronous (run in order, waiting for one command to finish before starting the next) unless you go out of your way to make it otherwise. – Charles Duffy Oct 20 '17 at 17:24
  • 1
    This shouldn't have been closed as duplicate. OP is asking for the equivalent of *javascript's* setTimeout, which is a non-blocking way to program a task to be run in a future. Not the same as sleep. – Edgar Villegas Alvarado Jun 01 '18 at 21:20
  • Assuming you want `python` to run in the foreground, then once it's done return to your bash shell and have `csv2xlsx_osx` run in the background (in parallel) 5 seconds later, you can do: `python script.py && { sleep 5 && ./csv2xlsx_osx -infile input.csv -outfile ouptut.xlsx & }`. The curly brackets form a bash command group (the spaces are important), and the `&` at the end makes the contents of the command group run in the background so it won't block bash. The command group will only start executing once the previous command (`python`) exits (and only if it exits successfully due to `&&`). – John Mellor Mar 11 '21 at 19:59

0 Answers0