Linux Unit Service遇到的坑

当将服务设置成开机启动的时候,执行如下命令:

 systemctl enable job.service

总是得到这样的报错:

Failed to enable unit: File job.service: Invalid argument

这个报错说无效参数,但提示信息非常不明显,于是去/var/log/syslog查看报错详情:

 systemd[1]: /etc/systemd/system/job.service:1: Missing '='.

丢失了“=”符号?然后看看我的unit service内容:

Unit]
Description=ndscheduler job service
After=network.target

[Service]
Type=simple
EnvironmentFile=/root/.bashrc
ExecStart=/usr/bin/python3 /root/src/ndscheduler/simple_scheduler/scheduler.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=job

仔细看了半天,也没发现哪里少了“=”号,google了半天这个异常,有说要用
systemd-analyze verify 检查文件:

systemd-analyze verify job.service                                                                                                                                                                           /etc/systemd/system/job.service:1: Assignment outside of section. Ignoring.
/etc/systemd/system/job.service:2: Assignment outside of section. Ignoring.
/etc/systemd/system/job.service:3: Assignment outside of section. Ignoring.

也只是模棱两可的提示

然后继续google啊google…一直都没找到答案,有说权限问题的,有说编码问题的,最后从这个“丑陋的”网页https://lists.freedesktop.org/archives/systemd-devel/2015-May/032029.html找到了启发:

他说还是他的文件有问题,是不是我的文件哪里写错了呢?于是我用BeCompare比较了job.service和另外一个正常的service内容:


原来是[unit]这里少了一个左方括号!!!

最后又执行了一遍systemctl enable job.service成功了

systemctl enable job.service
Created symlink /etc/systemd/system/job → /etc/systemd/system/job.service.
Created symlink /etc/systemd/system/multi-user.target.wants/job.service → /etc/s                                                                                                                                                          ystemd/system/job.service.

可是这样为什么执行systemctl start job.service不会报错呢?

这么不严谨的吗???

还是systemd容错性太强大了呢?

所以,遇到Missing ‘=’这种报错,还是得从文件内容着手,仔细检查哪里有写错。

发表评论

电子邮件地址不会被公开。 必填项已用*标注