B4J Question Linux web-app autostart

peacemaker

Expert
Licensed User
Longtime User
Hi, All

How to make sure that app is autostarted undex Linux ?

Tried to use crontab - does not start after reboot.

crontab -e
@reboot nohup /var/www/project/data/www/project/jdk-14.0.1/bin/java -jar /var/www/project/data/www/project/result.jar > nohup.out &

App is started with nohup by this above command only if to start manually from the app folder.
Ubuntu 22.04 server VPS (not desktop), if important.
SUDO before command does not help also.

Any suggestions ?
 

aeric

Expert
Licensed User
Longtime User
I created a script in home directory and call in crontab

startup.sh:
#!/bin/bash
cd /home/aeric
nohup /usr/bin/java -jar myapp.jar > nohup.out &

Bash:
$ sudo crontab -e

crontab:
@reboot sudo /home/aeric/startup.sh
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, Aeric, but if to start...

startup.sh: command not found

Rights are 755.

Script is re-typed anew on LInux:
1662870507064.png

manually started, but not from startup.sh
:)
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I store the file inside home
 
Upvote 0

Jones Hone

Active Member
Licensed User
Longtime User
The following is my practice, for your reference.
Not sure it's also suitable for your environment.
*.Please pay attention to whether the firewall has opened the port you want to use.
0. my environment:
Oracle Cloud Free Tier VPS: Ubuntu 20.04.3 LTS
My web-app in /home/ubuntu/b4jwebhook/WebHook.jar
1.create mywebhook.service file:
sudo vim /etc/systemd/system/mywebhook.service <Enter>
2. The content of mywebhook.service is as follows:
[Unit]
Description=This is my webhook
After=network.target
[Service]
# User=user
# Group=group
WorkingDirectory=/home/ubuntu/b4jwebhook
ExecStart=sh /home/ubuntu/b4jwebhook/webhookrun.sh
Restart=always
RestartSec=3s
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
3.After the file is created, execute the following command:
sudo chmod 755 /etc/systemd/system/mywebhook.service <Enter>
sudo systemctl daemon-reload<Enter>
sudo systemctl enable mywebhook.service<Enter>
4.create webhookrun.sh file:
sudo vim /home/ubuntu/b4jwebhook/webhookrun.sh <Enter>
5. The content of webhookrun.sh is as follows:
nohup sudo java -jar WebHook.jar &
6.After the file is created, execute the following command:
sudo chmod 755 /home/ubuntu/b4jwebhook/webhookrun.sh <Enter>

7.Ok. Now reboot the VPS, The web server is autostart.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Ya, sometimes we need to take care of file permission issue if something not work.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I remember I read somewhere that /home directory is mounted earlier when system booted than other directory so it is a better place to store the sh file.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I store the file inside home
Thanks. What is rights of this home/user folder ?
I re-installed the server, installed FTP, created a user, connected by FTP, but files are not saved into /home/user...
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks. What is rights of this home/user folder ?
I re-installed the server, installed FTP, created a user, connected by FTP, but files are not saved into /home/user...
Not sure but if you are logged in as root then should be no issue with access rights.
 
Upvote 0

teddybear

Active Member
Licensed User
I don't know which linux you use.
Try add startup script in /etc/rc.local file, or do it by Post#5
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

aeric

Expert
Licensed User
Longtime User
but using a script, I can run multiple instances like:
startup.sh:
#!/bin/bash
cd /home/b4j/app1
nohup /usr/bin/java -jar app1.jar > app1.out &
cd /home/b4j/app2
nohup /usr/bin/java -jar app2.jar > app2.out &
cd /home/b4j/app3
nohup /usr/bin/java -jar app3.jar > app3.out &
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
startup.sh: command not found
by the way, if you are not using crontab but to manuallly run, then I think the command is :
B4X:
$ sh startup.sh
or
B4X:
$ ./startup.sh
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
$ sh startup.sh
Yes, it works, but again strange that 2 instances of Java (excepting auto-java process), after run the .sh script:
1662988218089.png

If to kill any of these processes - all are killed.
 
Upvote 0

teddybear

Active Member
Licensed User
Yes, it works, but again strange that 2 instances of Java (excepting auto-java process), after run the .sh script:
View attachment 133553
If to kill any of these processes - all are killed.
I don't know how you write handler(webserver). that 2 instances are child processes forked by result.jar(pid is 25886, ppid is 1) you can see pid and ppid, usually, that's because there are clients connected to the webserver.
also you can use ps -ef|grep java|grep -v grep to filter the auto java
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
but using a script, I can run multiple instances
Yes, as long as you're ok of not using systemd commands to control (start / stop / restart / etc) the individual instances.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User

SOLVED: at last helped this https://www.b4x.com/android/forum/threads/linux-web-app-autostart.142854/post-905176, but with WorkingDirectory=/work/www/myunit

[Unit]
Description=sc4 Service
After=network.target

[Service]
Type=simple
WorkingDirectory=/home/vlad/web/scat.team
ExecStart=/home/vlad/web/scat.team/jdk-14.0.1/bin/java -jar /home/vlad/web/scat.team/result.jar
SuccessExitStatus=143

Restart=on-abort

[Install]
WantedBy=multi-user.target
 
Last edited:
Upvote 0
Top