how do i run a script 5mn after startup?

8

I have severals RoR websites running on my webserver and I need to run a ruby script at startup.

So, I puted a bash script in /etc/init.d witch call a ruby process. Unfortunatly that ruby process needs some stuff that isn't loaded yet.

I tried to add a sleep 300 at the beginning of my bash script, but it doesn't really help because it also affect the startup of stuff like ssh, apache, etc...

Also, I don't want to load all the ruby libraries at startup, because it would slow down the boot time.

boby lapointe

Posted 2012-08-10T16:09:21.527

Reputation: 203

Answers

10

sleep 300 is the way to go, but you need to put it in a function which you will call asynchroneous:

myscript()
{
    sleep 300

    # do what you want
}

myscript &

# continue with other things

Tamara Wijsman

Posted 2012-08-10T16:09:21.527

Reputation: 54 163

6

Sleeping for five minutes is a pretty unstable hack. What if something later in the boot chain takes an unusal long time to start up? And why do you want your system to take longer than necessary to boot?

The right way to do it is to make your startup script run after the stuff it depends on. How you do this differs between distros.

In Debian you specify in the script header what dependencies your script have:. Here's an example from /etc/init.d/README in Debian Wheezy:

# Required-Start:    $remote_fs $syslog

On other systems you usually name the script with a number somewhere in the filename in one of the /etc/rc?.d/ folder. On such distros, just give it a higher number than the stuff it depends on.

Emil Vikström

Posted 2012-08-10T16:09:21.527

Reputation: 419

1It is unstable because usually delaying operations to fix a bug indicates a concurrency problem and adding a delay will work until your system gets slower/faster. – AsTeR – 2014-08-12T10:28:20.583

Why would it be unstable, sleep even provides 5m syntax. I agree dependencies are the way to go, although boots that take more than five minutes should probably looked at and the RoR script failing is a good way to be brought to attention (but not the right way). – Tamara Wijsman – 2012-08-13T08:47:52.880

0

I wonder if you could start another thread and the first thing that thread does is sleep 300. Then it would call the script that you would have called from init.d

I think it would be something like

newscript &

AeroJoe

Posted 2012-08-10T16:09:21.527

Reputation: 21

0

create file: delay

#!/bin/bash
# $1:delay time(s)
# $2:command
# example:
#    delay 10 "conky -d"

sleep $1
exec $2

then

chmod a+x delay
delay 10 "xxx"

put it in /etc/rc.local

NineHills

Posted 2012-08-10T16:09:21.527

Reputation: 28