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 ?
 
I created a script in home directory and call in crontab

[CODE lang="bash" title="startup.sh"]#!/bin/bash
cd /home/aeric
nohup /usr/bin/java -jar myapp.jar > nohup.out &[/CODE]

Bash:
$ sudo crontab -e

[CODE lang="bash" title="crontab"]@reboot sudo /home/aeric/startup.sh[/CODE]
 
Upvote 0
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
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
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
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
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
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
Upvote 0
but using a script, I can run multiple instances like:
[CODE lang="b4x" title="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 &
[/CODE]
 
Upvote 0
$ 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
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
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

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
Back
Top