linux学习-将seafile启动脚本设置为开机启动服务

有时候,我们安装的linux软件和程序不是通过yum安装,而是通过编译或者其他方式安装。有时需要将程序设置为服务,达到开机启动的目的。

我在公有云的与服务器上搭建了seafile网盘,当我重启云服务器的时候,seafile的程序不会自动启动,需要我在相关目录下执行脚本,才能启动。

image.png

将两个脚本使用软连接到/root/目录下,方便执行

image.png

现在采用服务的方式,将这这两个脚本加入开机启动


创建 systemd 服务文件 /etc/systemd/system/seafile.service

vim /etc/systemd/system/seafile.service 
=======================内容如下========================
[Unit]
Description=Seafile
# add mysql.service or postgresql.service depending on your database to the line below
After=network.target

[Service]
Type=oneshot
ExecStart=/home/cloud_storage/seafile-server-latest/seafile.sh start
ExecStop=/home/cloud_storage/seafile-server-latest/seafile.sh stop
RemainAfterExit=yes
User=seafile
Group=seafile

[Install]
WantedBy=multi-user.target


这个文件由三个部分组成:Unit\Service\Install

[Unit]主要是为了解决依赖关系。常见的添加Requires、After,如果这个依赖是可选的,那么是Wants、After。依赖关系通常被用在服务(service)而不是(target)上,所以上述的httpd所依赖的仅仅是一些target,因而也就没有Requires和Wants出现。

[service]可选择几种不同的服务启动方式,启动方式通过Type参数进行设置。

Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。

Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。

Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪

[Install]

WantedBy=multi-user.target多用户启动



创建 systemd 服务文件 /etc/systemd/system/seahub.service

vim /etc/systemd/system/seahub.service
=======================内容如下========================
[Unit]
Description=Seafile hub
After=network.target seafile.service

[Service]
# change start to start-fastcgi if you want to run fastcgi
ExecStart=/home/cloud_storage/seafile-server-latest/seahub.sh start
ExecStop=/home/cloud_storage/seafile-server-latest/seahub.sh stop
User=seafile
Group=seafile
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target


重新加载服务

systemctl daemon-reload
systemctl enable seafile.service
systemctl enable seahub.service


猜你喜欢

转载自blog.51cto.com/11555417/2151938