How could I run a shell script with delay

34,156

Solution 1

In order to make a script run on startup first make it executable:

$ sudo chmod 755 /etc/init.d/znodejs.sh

Then you can register the script to be run at startup:

$ sudo update-rc.d znodejs.sh defaults

(Edit) original answer:

the sleep command sill pause for a given number of seconds:

sudo ./znodejs.sh stop
sleep 10
sudo ./znodejs.sh start

Solution 2

The standard unix command for sleeping is called

sleep

to wait a second, use

sleep 1
Share:
34,156
Jorge Cordero
Author by

Jorge Cordero

Updated on September 12, 2020

Comments

  • Jorge Cordero
    Jorge Cordero over 3 years

    I basically want to run a script which is a server but with 10 second delay, it is because I need some stuff to run before this script.

    The server is located in the folder /etc/init.d but basically to make it work I go to that path using the command line and I have to restart the server typing:

    sudo ./znodejs.sh stop
    

    And then I start the server again:

    sudo ./znodejs.sh start
    

    I would like to know if there is any way to run those commands with a delay.

  • Jorge Cordero
    Jorge Cordero over 8 years
    I think my question wasn't clear at all, the server starts automatically from the start, so I would like to create a script to run those commands with a delay. I don't want to type it in the command line every time my raspberry reboot. So I tried to run it like this in the rc.local: (sleep 10; sudo /etc/init.d/znodejs.sh stop) (sleep 10; sudo /etc/init.d/znodejs.sh start)
  • Chris Montanaro
    Chris Montanaro over 8 years
    @JorgeCordero I have updated my answer to show how to enable your script to run on startup