0

Via my host I have SSH access and a control panel. I installed Rails via SSH on a light weight server. I want to reboot the server when it goes down. I think I need to start it manually since I want to start two servers which run on different versions of Ruby.

I was wondering if I can use the "Cronjob" area on my control panel for this, and if so, what command I need.

Via SSH I would start the server as follows:

cd [app1path] 
bundle exec thin -C /etc/thin/app2.yml start

cd [app2path]
bundle exec thin -C /etc/thin/app2.yml start

It is important that the steps are executed in sequence, since they seem to conflict with each other (running them as a service from init.d only starts one, strangely enough placing a sleep 60 in one of the two does not make a diffrence, but anyway).

Placing the code below in my control panel does not seem to do anything:

@reboot [app1path; bundle exec thin -C /etc/thin/app1.yml start; cd [app2path]; bundle exec thin -C /etc/thin/app2.yml start

What would work? I would be grateful for some tips and guidance.

3
  • If you run systemd, this is a task for a service file, not a cron job...
    – jasonwryan
    Sep 6, 2014 at 20:09
  • @jasonwryan I have tried to do this as well. See this question on SO. There I run into the problem that only one of the two service files is executed (e.g., if I delete one the other works, otherwise I believe it is alphabetically). Sep 6, 2014 at 20:12
  • So those are sysvinit services (which doesn't handle this sort of requirement anywhere near as intelligently as systemd .service files).
    – jasonwryan
    Sep 6, 2014 at 20:15

1 Answer 1

2

THe @reboot entry is started when cron is started, but that doesn't mean everything necessary to run your bundle application is up and running. Depending on the setup, e.g. your network might not be up at that time.

I would do the following in your case:

  1. have the reboot job write a unique file in some known place
  2. have a normal cronjob that executes on a regular basis, e.g. every X minutes. This job checks if the unique file exists, and has existed for at least Y minutes.

If the file exists and is old enough, the file gets deleted and the commands to start the bundle are executed. With the checks you can be sure that your command does get started only once, roughly between Y and X+Y minutes after reboot. You can probably reduce X to 1 and Y to 1 or 2, giving a delay of 1 to 2-3 minutes after reboot (you should take larger values if your machine takes longer to get fully up and running).

An alternative is to create an init.d job yourself and insert that with appropriate links so that all necessary service are started before that. (How to do that depends on what kind of system your Debian system uses: systemd, sysvinit or some other)

2
  • init job is what you are looking for, not cron.
    – Archemar
    Sep 7, 2014 at 8:58
  • @Archemar I agree an init job is cleaner, but also requires more system related knowledge than general (shell) programming skills. For cron jobs that is the other way around (IMHO).
    – Anthon
    Sep 7, 2014 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .