Setting Up Custom Programs as Services for Automatic Startup in Linux

There are many ways to set custom programs to start automatically on boot in Linux systems. The method described in this article is to set the program as a service for automatic startup.Create a new hello.service file in the /lib/systemd/system directory.

touch /lib/systemd/system/hello.service
vim /lib/systemd/system/hello.service
[Unit]
Description=Hello Service

[Service]
Type=simple
ExecStart=/usr/bin/hello.sh
SuccessExitStatus=2

[Install]
WantedBy=multi-user.target

Note that when the system service process is completed in the program specified in ExecStart, the Type=simple configuration should be used.

If the program in ExecStart has child processes running in the background, the Type=forking configuration should be used, allowing the child processes to continue running in the background when the parent process exits.Create a startup script file hello.sh and grant executable permissions.

Setting Up Custom Programs as Services for Automatic Startup in Linux

touch /usr/bin/hello.sh
vi /usr/bin/hello.sh
#!/bin/bash
echo "Hello World!" > /home/root/hello
chmod +x /usr/bin/hello.sh
systemctl daemon-reload
systemctl enable hello.service

Note that in addition to using the systemctl command to set the service, you can also use the soft link ln command to set it up.

cd /etc/systemd/system/multi-user.target.wants
ln -s /lib/systemd/system/hello.service hello.service

Setting Up Custom Programs as Services for Automatic Startup in Linux

Copyright Statement: The content of this article comes from Blog Garden: Thousand_Mesh, and follows the CC 4.0 BY-SA copyright agreement. This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 2.5 Mainland China License. Original link: https://www.cnblogs.com/wzihan/p/18006837. If there is any infringement, please contact us, and it will be deleted immediately. Special thanks to the original author for their creation. All copyrights of this article belong to the original author and are unrelated to this public account. For commercial reprints, please contact the original author; for non-commercial reprints, please indicate the source.

Leave a Comment