2

I want to start java application on system boot using systemd. I tried to do by adding above scripts. But service is not starting.

my-startup.service

[Unit]
Description=Startup

[Service]
ExecStart=/usr/local/sbin/my-startup.sh

[Install]
WantedBy=multi-user.target

my-startup.sh

cd /var/www/test.com
nohup java -jar *.jar>test.out 2>test.err &

But application not starting when server starting.

● my-startup.service - Startup
   Loaded: loaded (/etc/systemd/system/my-startup.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2018-12-12 16:22:52 +0530; 27s ago
  Process: 650 ExecStart=/usr/local/sbin/my-startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 650 (code=exited, status=0/SUCCESS)

Dec 12 16:22:52 localhost.localdomain systemd[1]: Started Startup.
Dec 12 16:22:52 localhost.localdomain systemd[1]: Starting Startup...
Janith
  • 223
  • 2
  • 4
  • 8

1 Answers1

6

Using systemd directives in place of shell results in a more consistent environment. This includes not relying on $PATH or the working directory by providing full paths where they are known.

Neither & for job control, nor managing SIGHUP is needed. systemd service units are already backgrounded.

Assuming the program doesn't fork, in other words the java exec keeps running, that's Type=simple

[Unit]
Description=something

[Service]
ExecStart=/usr/bin/java -jar /var/www/test.com/something.jar
StandardOutput=file:/var/log/something.out.txt
StandardError=file:/var/log/something.err.txt
Type=simple
WorkingDirectory=/var/www/test.com

[Install]
WantedBy=multi-user.target
John Mahowald
  • 32,050
  • 2
  • 19
  • 34
  • This scrips is working fine. But there is a problem in `StandardOutput=file:/var/log/something.out.txt` `StandardError=file:/var/log/something.err.txt`. Not created `err` & `out` files. – Janith Dec 13 '18 at 02:47
  • I want to put my `test.out` & `test.err` inside of project directory. `/var/www/test.com`. Is it possible? – Janith Dec 13 '18 at 03:09
  • Your program would have to write something to stdout or stderr, and the unit would need to be started by a user with permission to write there (root). Put things where you want, dump it all in /opt or wherever. This is just an example that mostly follows conventions. – John Mahowald Dec 13 '18 at 05:06
  • I put `StandardOutput=file:/var/www/test.com/test.out.txt` but it doesn't write the log into file. Server starting works fine – Janith Dec 13 '18 at 05:20