dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Crontab, Screen and Java

Mon Apr 11, 2016 9:43 pm

Hello!

I've searched a lot but still didn't find the solution to my problem :cry:

I have a Java application which works perfectly on a detached screen when I run the command "screen -S twitter java -jar /home/pi/Twitter\ Bot\ Server.jar" manually. I want it to be executed at reboot.

I am using crontab to achieve it, so far I have created a script named "launch.sh" (content here : http://pastebin.com/WygEPp0Y) in the home directory (it works when running "sh launch.sh"), and I added this line to crontab : "@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1".

The problem is that nothing happen at reboot :?
Does anyone has advice? I would really appreciate it.

Thanks in advance,
dbrazzor

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Crontab, Screen and Java

Mon Apr 11, 2016 11:27 pm

dbrazzor wrote:I have a Java application which works perfectly on a detached screen when I run the command "screen -S twitter java -jar /home/pi/Twitter\ Bot\ Server.jar" manually. I want it to be executed at reboot.

I am using crontab to achieve it,
The problem usually involves the environment or permissions when crontab fails. Try doing something simple that could show that it is starting like perhaps printenv into a file. If that makes sense, does java by itself start?

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Crontab, Screen and Java

Tue Apr 12, 2016 12:48 am

the combination of screen and sudo (?? why) in cron's reduced PATH will be killing it
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Crontab, Screen and Java

Tue Apr 12, 2016 10:43 am

Thanks for your answers!

stderr : This is what I get when printenv runs with contab : http://pastebin.com/xFkLZs7g
I can get the java application running without screen, it must be the problem :?

scruss : The application use WiringPi and needs to be executed with sudo, do crontab executes commands with the same permissions? (sorry for my bad knowledge, I'm new to this). Still, nothing happens, even if I remove sudo :(

Regards,
dbrazzor

mutley
Posts: 61
Joined: Sat Jan 02, 2016 8:06 pm

Re: Crontab, Screen and Java

Thu Apr 14, 2016 1:20 pm

As long as you are using root's crontab, you won't need the sudo command. As an FYI, using sudo in a script is not the best approach, you should use sudo to run the actual script if it needs elevated privileges.

It looks like the path may be you problem.
put the full path to the screen / java commands in your script.

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Crontab, Screen and Java

Thu Apr 14, 2016 2:05 pm

Thanks for your answer!

I changed the script (which looks like this now : http://pastebin.com/cEvNm1jM)

But nothing changed, I still don't have any detached screen on reboot :?

It works perfectly without screen, maybe a problem with the environment?
I saw some posts saying the crontab line should starts with "(. ~/.profile;", can someone explains me what it does and what is the default profile name?

Thanks a lot!

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Crontab, Screen and Java

Thu Apr 14, 2016 3:12 pm

dbrazzor wrote: I saw some posts saying the crontab line should starts with "(. ~/.profile;", can someone explains me what it does and what is the default profile name?
That will source the old-style shell profile, which you may not have. It's supposed to set up your PATH so your cron command can find everything. It makes a lot of assumptions.

If you want to start something as root on boot, it can go in /etc/rc.local - but that has some limitations too.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Crontab, Screen and Java

Fri Apr 15, 2016 5:57 pm

scruss wrote: That will source the old-style shell profile, which you may not have. It's supposed to set up your PATH so your cron command can find everything. It makes a lot of assumptions.

If you want to start something as root on boot, it can go in /etc/rc.local - but that has some limitations too.
You could also join the 21st century and write a systemd service file.

viewtopic.php?p=949137#p949137
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Crontab, Screen and Java

Sat Apr 16, 2016 12:02 am

DougieLawson wrote:You could also join the 21st century and write a systemd service file.
One could, but it's not as straightforward as adding a line of shell commands to rc.local when you just need to run something. I'm sure I'll get to systemd when I need something more.

I wasn't, incidentally, recommending the . ~/.profile approach. Just that I've seen it suggested before.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Return to “General programming discussion”