B4J Question Run a Server on a VPS Linux

Philip Prins

Active Member
Licensed User
Hello

How to start the server after a reboot?
I made a CRON to start a script.
The script is started but not my .jar

In the script i placed : nohup /home/sever/bin/java -jar server.jar > nohup.out &

When running the script it works but not after reboot.
I made the Cron as root

Cron
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@reboot //home/server/bin/script.sh 2>&1 >> //home/server/bin/script.sh.log

The script log is changed after reboot but the .jar is not running.

Any help is highly apreciated
 

Philip Prins

Active Member
Licensed User
I followed an example that Erel posted to start the .jar file on Linux and remains running after you log out.

When i search the web on how to start after reboot i get thousands of problems and possible solutions.

Tried many of them, now have 6 clean new reinstall of Linux.

I have little to none knowledge of Linux and hope there are people on the forum that have a remote Linux Ubuntu server running with a .jar server made with B4J.

For me it has been frustrating the last 4 days trying something that should be so simple:Start the .jar again after reboot.
 
Upvote 0

derez

Expert
Licensed User
Is my script code correct to run B4J .jar from script.sh? nohup /home/server/bin/java -jar server.jar > nohup.out &
Yes, just make sure that the directories of java and the server are correct and that the server.jar is defined as executable (with its file properties). Also - add sudo first just to make sure.
 
Upvote 0

Philip Prins

Active Member
Licensed User
Yes, just make sure that the directories of java and the server are correct and that the server.jar is defined as executable (with its file properties). Also - add sudo first just to make sure.
The Cron and script are executed:
apr 29 12:10:52 h2883339.stratoserver.net cron[236]: (CRON) INFO (pidfile fd = 3)
apr 29 12:10:52 h2883339.stratoserver.net cron[236]: (CRON) INFO (Running @reboot jobs)
apr 29 12:10:52 h2883339.stratoserver.net CRON[257]: pam_unix(cron:session): session opened for user root by (uid
apr 29 12:10:52 h2883339.stratoserver.net CRON[275]: (root) CMD (/usr/scripts/script.sh> /usr/scripts/script.log

But the server is not running
When i run the script the server starts
 
Upvote 0

Sandman

Well-Known Member
Licensed User
Can you please confirm that we're talking about the file /etc/crontab ?

If so, perhaps you need to add one more thing to your command, from:
B4X:
@reboot //home/server/bin/script.sh 2>&1 >> //home/server/bin/script.sh.log
to
B4X:
@reboot root /home/server/bin/script.sh 2>&1 >> /home/server/bin/script.sh.log

Basically, I added the user that the script is meant to run as. (Change it to some other user if that's better suited.) And I removed the double slashes, I have no idea why you had them in there.

If that doesn't work, add this directly after the shebang:
B4X:
touch /tmp/cron_ran_fine
and after a boot check if that file is in /tmp

If that doesn't work, please do a
B4X:
ls -l /home/server/bin/script.sh && head -n1 /home/server/bin/script.sh
and post the output here
 
Upvote 0

Philip Prins

Active Member
Licensed User
Can you please confirm that we're talking about the file /etc/crontab ?

If so, perhaps you need to add one more thing to your command, from:
B4X:
@reboot //home/server/bin/script.sh 2>&1 >> //home/server/bin/script.sh.log
to
B4X:
@reboot root /home/server/bin/script.sh 2>&1 >> /home/server/bin/script.sh.log

Basically, I added the user that the script is meant to run as. (Change it to some other user if that's better suited.) And I removed the double slashes, I have no idea why you had them in there.

If that doesn't work, add this directly after the shebang:
B4X:
touch /tmp/cron_ran_fine
and after a boot check if that file is in /tmp

If that doesn't work, please do a
B4X:
ls -l /home/server/bin/script.sh && head -n1 /home/server/bin/script.sh
and post the output here
I moved everything to the folder usr

ls -l /usr/scripts/script.sh && head -n1 /usr/scripts/script.sh
Output:
-rwxrwxrwx 1 root root 118 apr 29 12:43 /usr/scripts/script.sh
#! /bin/sh


ls -l /usr/CommCheck/bin/script.sh && head -n1 /usr/CommCheck/bin/script.sh
Output
-rwxrwxrwx 1 root root 104 apr 29 12:45 /usr/CommCheck/bin/script.sh
 
Upvote 0

Sandman

Well-Known Member
Licensed User
The shebang in /usr/scripts/script.sh is wrong, it shouldn't have a space in it. Remove that and try again.

/usr/CommCheck/bin/script.sh seems to be missing a shebang altogether. Add that and try again.

If none of these worked, try changing the shebang to #!/bin/bash and see if it makes a difference.

Also try the touch thing I wrote about previously.

Also be aware that the environment in your scripts might be different from when you manually run the script so you can't assume that all system variables are the same. This means, for instance, that you might have to write out full paths to binaries you use in the scripts. So, for instance, this might not work:
B4X:
echo "hello" | rev
But this might work just fine:
B4X:
echo "hello" | /usr/bin/rev

Remember, you can always get the path to the binaries like so:
B4X:
which rev
/usr/bin/rev
 
Upvote 0

Philip Prins

Active Member
Licensed User
The shebang in /usr/scripts/script.sh is wrong, it shouldn't have a space in it. Remove that and try again.

/usr/CommCheck/bin/script.sh seems to be missing a shebang altogether. Add that and try again.

If none of these worked, try changing the shebang to #!/bin/bash and see if it makes a difference.

Also try the touch thing I wrote about previously.

Also be aware that the environment in your scripts might be different from when you manually run the script so you can't assume that all system variables are the same. This means, for instance, that you might have to write out full paths to binaries you use in the scripts. So, for instance, this might not work:
B4X:
echo "hello" | rev
But this might work just fine:
B4X:
echo "hello" | /usr/bin/rev

Remember, you can always get the path to the binaries like so:
B4X:
which rev
/usr/bin/rev
Ok the file is written in tmp

But the server won't start:

This is the script:
#!/bin/sh
touch /tmp/cron_ran_fine
echo "Start script"
sleep 5
nohup /usr/CommCheck/bin/java -jar CommCheck.jar > nohup.out &
echo "running"

Output:
root@h2883339:~# /usr/scripts/script.sh
Start script
running
root@h2883339:~# nohup: redirecting stderr to stdout

When starting the script manual it will not start.
 
Upvote 0

Philip Prins

Active Member
Licensed User
Ok the file is written in tmp

But the server won't start:

This is the script:
#!/bin/sh
touch /tmp/cron_ran_fine
echo "Start script"
sleep 5
nohup /usr/CommCheck/bin/java -jar CommCheck.jar > nohup.out &
echo "running"

Output:
root@h2883339:~# /usr/scripts/script.sh
Start script
running
root@h2883339:~# nohup: redirecting stderr to stdout

When starting the script manual it will not start.

When using nohup /usr/CommCheck/bin/java -jar CommCheck.jar > nohup.out &

Output
[1] 495
root@h2883339:~# nohup: ignoring input and redirecting stderr to stdout

[1]+ Exit 1 nohup /usr/CommCheck/bin/java -jar CommCheck.jar > nohup.out
 
Upvote 0

Sandman

Well-Known Member
Licensed User
I'm not entirely sure you need the nohup in your scenario. It's typically used to keep things running if the user session is killed, and as your scenario doesn't require us to have a user session that seems to not be relevant. I doubt it can harm though, but I'm not sure.

I would give this a shot and see what happens. Please note that I added the full path to CommCheck.jar (adjust per your situation).
B4X:
#!/bin/sh
touch /tmp/cron_ran_fine
echo "Start script" >> /tmp/cron_ran_fine
sleep 5
/usr/CommCheck/bin/java -jar /path/to/the/jar/CommCheck.jar &
echo "running" >> /tmp/cron_ran_fine

By the way, do you imagine that you at some point will start using the code tags in the forum? I'm making an effort to make my posts look better and be easier to read and understand. It would be nice if you did the same.
 
Upvote 0

OliverA

Expert
Licensed User
Have you ever checked the content of nohup.out? If CommCheck has an issue launching (or /usr/CommCheck/bin/java has an issue launching your .jar file), nothing will show up in STDOUT/STDERR for those two applications since you (via nohup) are redirecting their output to nohup.out (unless you once more redirect them in CommCheck to another file. If so, also check that file).
 
Upvote 0

Philip Prins

Active Member
Licensed User
I'm not entirely sure you need the nohup in your scenario. It's typically used to keep things running if the user session is killed, and as your scenario doesn't require us to have a user session that seems to not be relevant. I doubt it can harm though, but I'm not sure.

I would give this a shot and see what happens. Please note that I added the full path to CommCheck.jar (adjust per your situation).
B4X:
#!/bin/sh
touch /tmp/cron_ran_fine
echo "Start script" >> /tmp/cron_ran_fine
sleep 5
/usr/CommCheck/bin/java -jar /path/to/the/jar/CommCheck.jar &
echo "running" >> /tmp/cron_ran_fine

By the way, do you imagine that you at some point will start using the code tags in the forum? I'm making an effort to make my posts look better and be easier to read and understand. It would be nice if you did the same.
Yes That works,
Thank you very much.

Will do the quotes
Have you ever checked the content of nohup.out? If CommCheck has an issue launching (or /usr/CommCheck/bin/java has an issue launching your .jar file), nothing will show up in STDOUT/STDERR for those two applications since you (via nohup) are redirecting their output to nohup.out (unless you once more redirect them in CommCheck to another file. If so, also check that file).
Ok will check
 
Upvote 0
Top