Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question

marked as duplicate by Iain, Jenny D, Tim Brigham, mdpc, Ward Jul 23 '13 at 1:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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. –  Jan Wikholm Jul 22 '13 at 5:17
add comment

2 Answers 2

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.

share|improve this answer
    
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 '13 at 4:09
    
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. –  Jan Wikholm Jul 22 '13 at 4:23
    
Update above in main post –  Atomiklan Jul 22 '13 at 4:39
    
Found the answer, thanks for the help anyways. –  Atomiklan Jul 22 '13 at 5:23
    
Please do answer yourself and select it as the correct answer so this does not get left 'unanswered' :) –  Jan Wikholm Jul 22 '13 at 6:05
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.