当前位置: 首页 > 知识库问答 >
问题:

在CentOS 7上使用systemctl创建服务

农明辉
2023-03-14

我正在尝试在我的CentOS 7服务器上使用systemctl创建服务。

在/usr/lib/systemd/system中,我遇到了这种情况。。。

[root@centos-4gb-hel1-1 system]# pwd
/usr/lib/systemd/system
[root@centos-4gb-hel1-1 system]# ls -la getUpdatesTelegram.sh
-rwxr-xr-x. 1 root root 116 Oct 24 22:32 getUpdatesTelegram.sh
[root@centos-4gb-hel1-1 system]# cat getUpdatesTelegram.sh
#!/bin/bash
while true ; do
   /var/www/html/OpenProntoSoccorsi/TelegramBot/getUpdates_launcher.sh
   sleep 5
done
[root@centos-4gb-hel1-1 system]# 

如果我试图执行

   /var/www/html/OpenProntoSoccorsi/TelegramBot/getUpdates_launcher.sh

一切正常。

在/etc/systemd/system中,我有这个。。。。

[root@centos-4gb-hel1-1 system]# cd /etc/systemd/system
[root@centos-4gb-hel1-1 system]# ls -la
total 40
drwxr-xr-x. 10 root root 4096 Oct 24 22:05 .
drwxr-xr-x.  4 root root 4096 Oct 18 22:41 ..
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 basic.target.wants
lrwxrwxrwx.  1 root root   37 Sep 24 12:29 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 default.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:27 dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 getty.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 local-fs.target.wants
drwxr-xr-x.  2 root root 4096 Oct 24 22:32 multi-user.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:31 sysinit.target.wants
drwxr-xr-x.  2 root root 4096 Sep 24 12:26 system-update.target.wants
[root@centos-4gb-hel1-1 system]# cd multi-user.target.wants/
[root@centos-4gb-hel1-1 multi-user.target.wants]# ls -la
total 8
drwxr-xr-x.  2 root root 4096 Oct 24 22:32 .
drwxr-xr-x. 10 root root 4096 Oct 24 22:05 ..
lrwxrwxrwx.  1 root root   38 Sep 24 12:27 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:31 brandbot.path -> /usr/lib/systemd/system/brandbot.path
lrwxrwxrwx.  1 root root   39 Sep 24 12:27 chronyd.service -> /usr/lib/systemd/system/chronyd.service
lrwxrwxrwx.  1 root root   44 Sep 24 12:33 cloud-config.service -> /usr/lib/systemd/system/cloud-config.service
lrwxrwxrwx.  1 root root   43 Sep 24 12:33 cloud-final.service -> /usr/lib/systemd/system/cloud-final.service
lrwxrwxrwx.  1 root root   48 Sep 24 12:33 cloud-init-local.service -> /usr/lib/systemd/system/cloud-init-local.service
lrwxrwxrwx.  1 root root   42 Sep 24 12:33 cloud-init.service -> /usr/lib/systemd/system/cloud-init.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:26 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx.  1 root root   50 Oct 24 22:32 getUpdatesTelegram.service -> /usr/lib/systemd/system/getUpdatesTelegram.service
lrwxrwxrwx.  1 root root   42 Sep 24 12:27 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:29 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx.  1 root root   40 Sep 24 12:26 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx.  1 root root   46 Sep 24 12:31 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx.  1 root root   39 Sep 24 12:26 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx.  1 root root   36 Sep 24 12:27 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx.  1 root root   37 Sep 24 12:26 tuned.service -> /usr/lib/systemd/system/tuned.service
[root@centos-4gb-hel1-1 multi-user.target.wants]# cat getUpdatesTelegram.service
[Service]
Type=simple
ExecStart=/bin/bash /bin/getUpdatesTelegram.sh

[Install]
WantedBy=multi-user.target
[root@centos-4gb-hel1-1 multi-user.target.wants]#

当我尝试执行时

systemctl enable getUpdatesTelegram.service

我获得

...
...
getUpdatesTelegram.service  loaded failed failed   getUpdatesTelegram.service
...
...

我的日志是如下。。。。

[root@centos-4gb-hel1-1 multi-user.target.wants]# journalctl -u getUpdatesTelegram.service
-- Logs begin at Fri 2018-10-19 20:09:02 CEST, end at Wed 2018-10-24 22:44:11 CEST. --
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Started getUpdatesTelegram.service.
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Starting getUpdatesTelegram.service...
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: getUpdatesTelegram.service: main process exited, code=exited, status=203/EXEC
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: Unit getUpdatesTelegram.service entered failed state.
Oct 24 21:44:10 centos-4gb-hel1-1 systemd[1]: getUpdatesTelegram.service failed.

我哪里错了?

提前谢谢你!

共有2个答案

冉伯寅
2023-03-14

作为Red Hat的分支,CentOS倾向于遵循类似的指导方针。

Red Hat system administrator’s guide(红帽系统管理员指南)针对您的情况提供了这一有用的段落。

顺便说一句,雅各布提出的解决方案是正确的。

郤仰岳
2023-03-14

首先,定制服务不应该放在usr/lib/systemd/system中,而应该放在etc/systemd/system中。shell脚本也一样,只需将它们放入/usr/local/sbin或其他文件中。

这不起作用的原因是服务文件中的脚本路径。服务错误:

ExecStart=/bin/bash /bin/getUpdatesTelegram.sh

真的应该是

ExecStart=/bin/bash /usr/lib/systemd/system/getUpdatesTelegram.sh

(尽管如我所写,不要将shell脚本放入/usr/lib/systemd/system)

 类似资料:
  • 我创建了一个脚本来部署必须在启动时运行的java程序。我编写了一个由脚本自定义的litte模板,并将其添加到 /etc/systemd/system/ServerName.service 我在visudo中添加了用户psadmin来运行systemctl,而不需要提示输入密码。这对于使用sudo systemctl start ServerName启动服务非常有效。服务然后,脚本重新加载syste

  • 本文向大家介绍Centos7使用docker搭建gitlab服务器,包括了Centos7使用docker搭建gitlab服务器的使用技巧和注意事项,需要的朋友参考一下 了解到docker的优点,搭建快,运行要求资源低,最重要的是实现的功能和效果都能达到预期,于是决定使用docker来搭建gitlab服务器。 效果图如下: 系统环境:CentOS Linux release 7.1.1503 (Co

  • 我正在使用不和谐的网络钩子应用编程接口向我的不和谐服务器通道发送消息。在一个新的用例中,我想创建一个带有网络钩子的新频道。不幸的是,我找不到任何API这样做。我看了整个留档https://discordapp.com/developers/docs/resources/webhook#create-webhook 这有可能做到吗?我看到了一个允许它的不和谐机器人的方法——因此我觉得用网络钩子也应该

  • 问题内容: 在我的Dockerfile中,我试图安装多个服务,并希望在启动容器时使它们全部自动启动。服务之一是mysql,当我启动容器时,我看不到mysql服务正在启动。当我尝试手动启动时,出现错误: Dockerfile: 我的start.sh文件: Docker构建: Docker运行: 我检查了图像,也无济于事。如何使用通过systemctl / service命令启动的服务启动容器。 问题

  • 在我的Dockerfile中,我试图安装多个服务,并希望在启动容器时使它们都自动启动。其中一个服务是mysql,当我启动容器时,我没有看到mysql服务启动。尝试手动启动时,出现错误: DockerFile: 我已经检查了映像,但这也没有帮助。如何用使用systemctl/service命令启动的服务启动容器。

  • 本文向大家介绍Centos7搭建FTP服务器,包括了Centos7搭建FTP服务器的使用技巧和注意事项,需要的朋友参考一下 从网上搜索了好多搭建Centos7搭建服务器的教程都没有成功唯独这个,利用Windows资源管理器连接测试成功。 一、通过yum安装vsftpd yum install -y vsftpd 二、修改vsftpd的配置文件 vi /etc/vsftpd/vsftpd.conf