Ubuntu将脚本部署成系统服务

Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with pid 1. This also helps use to manage system and application service on our Linux operating system.

We can also run any custom script as systemd service. It helps the script to start on system boot. This can be helpful for you to run any script which required to run at boot time only or to run always.

This tutorial coverts to run a shell script as Systemd service.

  1. Create a shell script
#!/bin/bash

DATE=`date '+%Y%m%d-%H%M%S'`

LogNameDATE=`date '+%Y%m%d'`

/home/kafka/kafka/bin/connect-standalone.sh  /home/kafka/kafka/config/connect-standalone.properties  /home/kafka/kafka/config/connect-debezium-postgresql.properties >>/root/PgsqlKafkaLog/log$LogNameDATE.log

  1. Create A SystemD File
    create a service file for the systemd on your system. This file must have .service extension and saved under the under /lib/systemd/system/ directory
    #/etc/systemd/system
sudo nano /lib/systemd/system/shellscript.service 

添加以下内容

[Unit]
Description=My Shell Script

[Service]
ExecStart=/usr/bin/script.sh
Restart=on-failure#always
RestartSec=3

[Install]
WantedBy=multi-user.target

关于python文件做成服务

[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target
  1. Enable New Service
    Your system service has been added to your service. Let’s reload the systemctl daemon to read new file. You need to reload this deamon each time after making any changes in in .service file.
sudo systemctl daemon-reload 

Now enable the service to start on system boot, also start the service using the following commands.

sudo systemctl enable shellscript.service 
sudo systemctl start shellscript.service 

Finally verify the script is up and running as a systemd service.

sudo systemctl status shellscript.service 

https://blog.csdn.net/weixin_43632687/article/details/123369843

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值