栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Ubuntu将脚本部署成系统服务

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.

    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

    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
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
    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 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/747654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号