1

What am I doing wrong here with cron on boot?

Example:

To launch manually: screen -S example ./script.sh
To recover manually: screen -x example

now, I just try to do the same thing (well, the launch part) using cron on boot

@reboot cd /home/administrator/script && screen -S example ./script.sh

I think it works without screen. For example:

@reboot cd /home/administrator/script && ./script.sh

but I also need this to run in the background, independent of the terminal. I have tried other options such as init, init.d, but there seems to be a lot of confusion out there on how it all works on which versions of linux. If you can fix my error above or show me a better solution, that would be great.

I am using Ubuntu 12.04.2 LTS

Thanks

* UPDATE *

So in response to Jan Wikholm, I have tried the absolute path and that completely fails.

Example

If I test this in the console:

screen -S example /home/administrator/script/script.sh

The script does not execute. Am I missing something conceptual here?

I get the screen error message [screen is terminating] immediately

* SOLUTION *

Finally figured it out. The issue with screen was the path to the executable.

Here is the final crontab entry that works like a charm

@reboot cd /home/administrator/script && /usr/bin/screen -dmS example ./script.sh

Thanks for the help Jan

1
  • 1
    I would recommend directing STDOUT and STDERR to some file to see if there is any errors. Also without seeing what script.sh contains it is hard to give detailed advice. Jul 22, 2013 at 5:17

2 Answers 2

1

Have you tried starting it with:

nohup /path/to/script/script.sh &> /output/goes/here &

Which would start the script, redirect both STDOUT and STDERR to the file, launch it to the background and tell it to ignore hangup signals (for its parent process dying).

Edit: Also it would be wiser to always wrap init/cron scripts so that the line cron has to invoke is itself just a script and not 'cd /somewhere && ./script.sh'. so it would be better if you just had /path/to/launcher.sh which then contained 'cd /somewhere && screen foobar' etc.

4
  • 1
    the script that it runs does not execute and finish with an output. It starts a server. Running in background did not seem to help.
    – Atomiklan
    Jul 22, 2013 at 4:09
  • 1
    I would recommend checking cron's and your server's logs to see if there is something else that is wrong. Most common pitfalls when using cron to start stuff is that PATH is not what it normally is and trying to use commands without their full paths will fail. Jul 22, 2013 at 4:23
  • 1
    Update above in main post
    – Atomiklan
    Jul 22, 2013 at 4:39
  • 1
    Please do answer yourself and select it as the correct answer so this does not get left 'unanswered' :) Jul 22, 2013 at 6:05
1

Finally figured it out. The issue with screen was the path to the executable.

Here is the final crontab entry that works like a charm

@reboot cd /home/administrator/script && /usr/bin/screen -dmS example ./script.sh

Thanks for the help Jan

PS, yeah tried to answer it, but it wouldn't let me for 8 hours so I just posted answer in original post for the time being.

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