如何让/etc/profile中的命令被找到?

如何让/etc/profile中的命令被找到?

操作系统:debian9。

touch /home/test/test.log

/etc/profile 中有一个简单的函数 write-date。

write-date(){
    date >>  /home/test/test.log
    }

创建在重新启动或关闭时运行的服务。

vim  /etc/systemd/system/test.service  

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash  /home/test/test.sh

[Install]
WantedBy=multi-user.target

/home/test/test.sh 中的简单函数 write-date。

vim  /home/test/test.sh
write-date

启用测试服务。

sudo systemctl enable test.service

重新启动我的电脑并检查 test.service 的日志

sudo journalctl -u test
-- Logs begin at Thu 2018-02-01 00:03:59 HKT, end at Thu 2018-02-01 00:15:54 HKT. --
Feb 01 00:04:04 test systemd[1]: Starting Run command at shutdown...
Feb 01 00:04:05 test bash[438]: /home/test/test.sh: line 3: write-date: command not found
Feb 01 00:04:11 test systemd[1]: test.service: Main process exited, code=exited, status=127/n/a
Feb 01 00:04:11 test systemd[1]: Failed to start Run command at shutdown.
Feb 01 00:04:11 test systemd[1]: test.service: Unit entered failed state.
Feb 01 00:04:11 test systemd[1]: test.service: Failed with result 'exit-code'.

如何让/etc/profile中的命令被找到?

cat -vet /home/test/test.sh
$
$
    write-date$
    $
$

答案1

您可能需要明确告诉您的服务它需要来源/etc/profile

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
EnvironmentFile=-/etc/profile
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash  /home/test/test.sh

[Install]
WantedBy=multi-user.target

注意该EnvironmentFile线。

相关内容