cron 中启动 apache 服务的两个命令,一个行,一个不行

2018-09-09 10:51:39 +08:00
 helloiac

写了个 shell 脚本,cron 每分钟执行,发现 apache2 进程停止了就去重启 apache2,尝试用了下面两个命令格式:

√ /etc/init.d/apache2 start

× service apache2 start

结果第一个可以,第二个不行。都是 root 权限,apache 的日志也都一样。

请教为什么。

# 进程测试 service apache2 start,结果进程未启动
[Sun Sep 09 10:29:27.037734 2018] [mpm_event:notice] [pid 17399:tid 3074283264] AH00491: caught SIGTERM, shutting down
[Sun Sep 09 10:30:30.292831 2018] [mpm_event:notice] [pid 17768:tid 3074914048] AH00489: Apache/2.4.34 (Ubuntu) OpenSSL/1.1.0h configured -- resuming normal operations
[Sun Sep 09 10:30:30.292967 2018] [core:notice] [pid 17768:tid 3074914048] AH00094: Command line: '/usr/sbin/apache2'

# 进程测试 /etc/init.d/apache2 start,进程启动了
[Sun Sep 09 10:40:49.433989 2018] [mpm_event:notice] [pid 17768:tid 3074914048] AH00491: caught SIGTERM, shutting down
[Sun Sep 09 10:41:03.682583 2018] [mpm_event:notice] [pid 18234:tid 3074279168] AH00489: Apache/2.4.34 (Ubuntu) OpenSSL/1.1.0h configured -- resuming normal operations
[Sun Sep 09 10:41:03.682709 2018] [core:notice] [pid 18234:tid 3074279168] AH00094: Command line: '/usr/sbin/apache2'
875 次点击
所在节点    问与答
2 条回复
bsder
2018-09-09 14:32:22 +08:00
因为 cron 中$PATH=/usr/bin:/bin。service 是 /usr/sbin 下命令。你要么写全路径,要么 cron 中设置一下$PATH。
helloiac
2018-09-09 14:40:35 +08:00
@bsder 学习了,谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/487558

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX