Systemctl
systemctl commands
# start systemctl start NAME.service # stop systemctl stop NAME.service # restart systemctl restart NAME.service # check status systemctl status NAME.service # View all activated system services systemctl list-units -t service # View all system services systemctl list-units -t service -a # Set boot autostart systemctl enable NAME.service # Disable auto-start at boot systemctl disable NAME.service # Check whether the service is automatically started at boot systemctl list-unit-files –t service # Check whether a service starts automatically at boot systemctl is-enabled NAME.service
systemctl status
loaded: unit configuration file has been processed active (running): one or more ongoing processing runs active (exited): successfully completed one-time configuration active (waiting): running, waiting for an event inactive: not running enable: boot up disable: do not start at boot static: not started at boot, but can be activated by another enabled service.
systemctl runlevel
0 : shutdown: poweroff.target 1: single user mode: rescue.target 2: Multi-user of NFS is not enabled by default: multi-user.target 3: Full multi-user: multi-user.target 4: reserved: mult-user.target 5 : Graphical: graphical.target 6: Reboot: reboot.target
Self-built service
1. Service storage path
User-level system services: /usr/lib/systemd/system System-level system services: /etc/systemd/system
2. Write a system service
The three parts of creating a system service file are: Unit, Service, Install
Unit:
Description: description information After: Define the startup order of the unit, indicating which units the current unit should start after, and its function is opposite to Before; Requires: Dependent on other units, strong dependence, when the dependent units cannot be activated, the current unit cannot be activated; Wants: Other units that depend on, weak dependencies; Conflicts: Define the conflict relationship between units.
Service:
Type: defines the unit process startup type that affects ExecStart and related parameter functions; simple: the default value, this daemon is mainly started by the command string connected to ExecStart, and resides in the memory after startup; forking: The program started by ExacStart extends other subroutines through spawns as the main service of this deamon. The native parent program terminates after startup. oneshot: similar to simple, but this program ends after completing the work, and does not reside in memory; dbus: Similar to simple, but this daemon must obtain a D-Bus name before continuing to operate. Therefore, it is usually necessary to set BusName= at the same time; notify: A notification message will be sent after the startup is complete. It is also necessary to cooperate with NotifyAccess to allow Systemd to receive messages; idle: Similar to simple, to execute this daemon, all tasks must be executed successfully. This type of daemon is usually a service that can only be executed at the end of the boot. EnvironmentFile: environment configuration file; ExecStart: Indicates the absolute path of the command or script to be run when starting the unit; ExecStartPre: the absolute path to run before ExecStart; ExecStartPost: the absolute path to run after ExecStart; ExecStop: Specifies the absolute path of the command or script to stop the unit to run; Restart: When Restart=1 is set, the daemon service will automatically start again after the unexpected termination of the daemon service. PrivateTmp: true/false indicates whether to allocate independent temporary space to the service Install: Alias: Alias, you can use systemctl command Alias.service RequiredBy: which units are dependent on, strong dependence; WantedBy: which units are dependent on, weakly dependent; Also: When installing this service, you need to install other related services.
Example
vim /usr/lib/systemd/system/wxybackend.service
:wq to quit
[Unit] Description=backend service After=docker.service [Service] Type=forking ExecStart=/home/project/xxx/syscmd.sh start ExecReload=/home/project/xxx/syscmd.sh restart ExecStop=/home/project/xxx/syscmd.sh stop PrivateTmp=true [Install] WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable wxybackend.service
systemctl start wxybackend.service
syscmd.sh script content
#!/bin/sh # start start stop stop restart restart status status AppName=xxxx.jar AppPath=/home/project/xxx # JVM parameters JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m -XX: + HeapDumpOnOutOfMemoryError -XX: + PrintGCDateStamps -XX: + PrintGCDetail s-XX :NewRatio=1 -XX:SurvivorRatio=30 -XX: + UseParallelGC -XX: +UseParallelOldGC" APP_HOME=`pwd` LOG_PATH=$AppPath/logs/$AppName.log if [ "$1" = "" ]; then echo -e "\033[0;31m No operation name entered \033[0m \033[0;34m {start|stop|restart|status} \033[0m" exit 1 the fi if [ "$AppName" = "" ]; then echo -e "\033[0;31m No application name entered \033[0m" exit 1 the fi function start() {<!-- --> PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'` if [ x"$PID" != x"" ]; then echo "$AppName is running..." else # output log nohup java $JVM_OPTS -jar $AppPath/$AppName >> $AppPath/nohup.out 2> & amp;1 & amp; #do not output log #nohup java $JVM_OPTS -jar $AppPath/$AppName & echo "Start $AppName success..." #tail -500f nohup.out the fi } function stop() {<!-- --> echo "Stop $AppName" PID="" query(){<!-- --> PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'` } query if [ x"$PID" != x"" ]; then kill -TERM $PID echo "$AppName (pid:$PID) exiting..." while [ x"$PID" != x"" ] do sleep 1 query done echo "$AppName exited." else echo "$AppName already stopped." the fi } function restart() {<!-- --> stop sleep 2 start } function status() {<!-- --> PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l` if [ $PID != 0 ]; then echo "$AppName is running..." else echo "$AppName is not running..." the fi } case $1 in start) start;; stop) stop;; restart) restart;; status) status;; *) esac
Run the jar file in the Linux environment
method one:
Command: java -jar xxxx.jar
Features: The current ssh window is locked, you can press ctrl + c to interrupt the running of the program, or close the window directly, and the program exits.
Method 2:
Command: java -jar xxxx.jar &
Features: & amp; means running in the background. The current ssh window is not locked, but when the window is closed, the program aborts.
Method 3:
Command: nohup java -jar xxxx.jar &
Features: nohup means not to hang up the running command, when the account is logged out or the terminal is closed, the program will still run. When executing a job with the nohup command, by default all output from the job is redirected to the nohup.out file, unless an output file is specified otherwise.
Method 4:
Command: nohup java -jar xxxx.jar >/log.file &
Features: output the log to the file specified by log.file.
>> output redirection 2> & amp;1 (2) standard error output (>) redirected to ( & amp;1) standard output 2> & amp;1 standard error output redirected to standard output & amp; identifies the process as a background process
In the shell command, several basic symbols and their meanings
0 means stdin standard input 1 means stdout standard output 2 means stderr standard error
expand:
Regardless of whether the output of the nohup command is redirected to the terminal, the output will be appended to the nohup.out file in the current directory. If the nohup.out file in the current directory is not writable, the output will be redirected to the $HOME/nohup.out file
The command is changed to nohup java -jar project.jar >> log.out 2> & amp;1 & amp; The problem is perfectly solved, the log is appended to the log.out file, and the thread runs in the background without hanging up
Error nohup can’t find java
Check if there is a problem with the environment variable or specify the path of jdk when the sh script is running