Просьба оставить комментарий





Если вам понравился или не понравился топик. Я что то забыл или не дописал, то вы можете оставить свой комментарий и я постараюсь исправить это в ближайшее время.

понедельник, 16 января 2023 г.

запуск java как сервис systemd

 

Step 1: Create an Application User and group

Start by creating a system group for the user.

sudo groupadd -r appmgr

Next, we create a system user jvmapps with the default group:

sudo useradd -r -s /bin/false -g appmgr jvmapps

Confirm user created and if with the correct group:

$ id jvmapps
uid=992(jvmapps) gid=986(appmgr) groups=986(appmgr)

Step 2: Create Systemd Service

We can now create a systemd service file to manage our application. You need to create this file as root user.

sudo vim /etc/systemd/system/myapp.service

It will have content like below:

[Unit]
Description=Manage Java service

[Service]
WorkingDirectory=/opt/prod
ExecStart=/bin/java -Xms128m -Xmx256m -jar myapp.jar
User=jvmapps
Type=simple
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Set User to the one created earlier, and WorkingDirectory to the directory with a jar file.

-Xms128m and  -Xmx256m are used to set the minimum and maximum memory that the application can use.

When done with the change, give the user and group ownership permissions for the Project Directory:

sudo chown -R jvmapps:appmgr /opt/prod

Step 3: Start Java Application service with systemd

The next thing to do is start the application service, but first, reload systemd so that it knows of the new application added.

sudo systemctl daemon-reload

Once reloaded, start the service:

sudo systemctl start myapp.service

To check the status, use:

$ systemctl status myapp

Sample output:

$ systemctl status myapp
● myapp.service - Manage Java service
   Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-08-24 11:12:23 EAT; 23h ago
 Main PID: 23220 (java)
   CGroup: /system.slice/myapp.service
           └─23220 /bin/java -Xms128m -Xmx256m -jar myapp.jar

Aug 25 10:50:00 server1 java[23220]: # Duration: 1000 µs
Aug 25 10:50:00 server1 java[23220]: # Total number of fields classified 0, 0 failed
Aug 25 10:50:00 server1 java[23220]: # ---------------------------------------------------------------
Aug 25 10:50:00 server1 java[23220]: # Completed step 'classify-tables'
Aug 25 10:50:00 server1 java[23220]: # Start: 2018-08-25T07:50:00.258Z
Aug 25 10:50:00 server1 java[23220]: # End: 2018-08-25T07:50:00.259Z
Aug 25 10:50:00 server1 java[23220]: # Duration: 1000 µs
Aug 25 10:50:00 server1 java[23220]: # Total number of tables classified 3, 0 updated
Aug 25 10:50:00 server1 java[23220]: #################################################################
Aug 25 10:50:00 server1 java[23220]: 08-25 10:50:00 INFO sync.util :: FINISHED: Analyze data for mysql Database 2 'Ch... (8 ms)
Hint: Some lines were ellipsized, use -l to show in full.

You can also enable the service to start on server boot:

$ sudo systemctl enable myapp
Created symlink from /etc/systemd/system/multi-user.target.wants/myapp.service to /etc/systemd/system/myapp.service.

To restart the application, use:

sudo systemctl restart myapp

Conclusion

You now have a Java Application being managed by Systemd. Replicate the same procedure for all other services you need to manage using Systemd init. Hope this was helpful, for any issues, let me know through the comments section.