编辑1个自定义systemd 服务并令其自启动

为什么突然搞起了systemd, 因为最近在研究GCP的 vm instance-template 和 系统image, 要制作1个自定义的系统image, 需要利用到systemd.




第一步, 添加1个.service 文件在 目录/etc/systemd/system/

gateman@tf-vpc0-subnet0-vm0:/etc/systemd/system$ pwd
/etc/systemd/system
gateman@tf-vpc0-subnet0-vm0:/etc/systemd/system$ ls
NetworkManager.service.wants  chronyd.service       getty.target.wants       network-online.target.wants  networking.service.wants  sshd.service        sysinit.target.wants  systemd-networkd.service.wants
basic.target.wants            default.target.wants  multi-user.target.wants  network.service.wants        sockets.target.wants      sshd.service.wants  syslog.service        timers.target.wants
gateman@tf-vpc0-subnet0-vm0:/etc/systemd/system$ sudo touch tomcat10.service
gateman@tf-vpc0-subnet0-vm0:/etc/systemd/system$ 




第二步,编辑这个.service 文件

下面我已tomcat10 作为例子

[Unit]
Description=Apache Tomcat Web Server 10
After=network.target

[Service]
Type=forking
User=gateman
Group=gateman
ExecStart=/home/gateman/server/tomcat10/bin/startup.sh
ExecStop=/home/gateman/server/tomcat10/bin/shutdown.sh
Restart=always

[Install]
WantedBy=multi-user.target

After=network.target 意思是开机启动时在network service 之后才启动

WantedBy=multi-user.target 意思是只在命令行开机模式启动

Type=forking 意思是, 启动startup.sh时会fork1个进程, 因为startup.sh 本来就是用另1个进程去启动tomcat, 而它本身会关闭




第三步,reload systemd 的 service list

sudo systemctl daemon-reload




第四步,设置tomcat10 service 自启动

gateman@tf-vpc0-subnet0-vm0:/etc/systemd/system$ sudo systemctl enable tomcat10
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat10.service → /etc/systemd/system/tomcat10.service




第五步,测试: 重启服务器

[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute instances reset tf-vpc0-subnet0-vm0
No zone specified. Using zone [europe-west2-c] for instance: [tf-vpc0-subnet0-vm0].
Updated [https://www.googleapis.com/compute/v1/projects/jason-hsbc/zones/europe-west2-c/instances/tf-vpc0-subnet0-vm0].




第五步,测试: 重新连接服务器, 检查tomcat 是否被启动

gateman@tf-vpc0-subnet0-vm0:~$ systemctl status tomcat10
● tomcat10.service - Apache Tomcat Web Server 10
     Loaded: loaded (/etc/systemd/system/tomcat10.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-12-14 17:41:30 UTC; 2min 29s ago
    Process: 499 ExecStart=/home/gateman/server/tomcat10/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 524 (java)
      Tasks: 34 (limit: 2353)
     Memory: 135.2M
        CPU: 5.898s
     CGroup: /system.slice/tomcat10.service
             └─524 /usr/bin/java -Djava.util.logging.config.file=/home/gateman/server/tomcat10/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 ->

Dec 14 17:41:29 tf-vpc0-subnet0-vm0 systemd[1]: Starting Apache Tomcat Web Server 10...
Dec 14 17:41:30 tf-vpc0-subnet0-vm0 startup.sh[499]: Tomcat started.
Dec 14 17:41:30 tf-vpc0-subnet0-vm0 systemd[1]: Started Apache Tomcat Web Server 10.
gateman@tf-vpc0-subnet0-vm0:~$ 
gateman@tf-vpc0-subnet0-vm0:~$ ps -ef | grep java
gateman      524       1  3 17:41 ?        00:00:05 /usr/bin/java -Djava.util.logging.config.file=/home/gateman/server/tomcat10/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED -classpath /home/gateman/server/tomcat10/bin/bootstrap.jar:/home/gateman/server/tomcat10/bin/tomcat-juli.jar -Dcatalina.base=/home/gateman/server/tomcat10 -Dcatalina.home=/home/gateman/server/tomcat10 -Djava.io.tmpdir=/home/gateman/server/tomcat10/temp org.apache.catalina.startup.Bootstrap start
gateman      958     952  0 17:44 pts/0    00:00:00 grep java
gateman@tf-vpc0-subnet0-vm0:~$ 

你可能感兴趣的:(linux,linux)