Shell 脚本未从 cron 运行

Shell 脚本未从 cron 运行

服务器 Ubuntu 16.04 在虚拟盒中运行。

我有一台需要重建的服务器(没有备份)。我复制了所有需要重用的相关脚本,除了从 root cron 调用的脚本外,其他所有脚本都按预期工作。这之前运行良好,没有问题。该脚本在屏幕会话中启动 jupyter notebook,手动执行时会执行其应执行的操作。脚本是:

#!/bin/bash
# cd /home/<username>/jupyternb
screen -S jupyter -dm jupyter notebook

crontab 行如下:

@reboot sh /home/<username>/.STARTUP_SCRIPTS/start_jupyter.sh

(<用户名> 是实际用户)

shell脚本权限为777。

syslog 显示该脚本正在以 root 身份调用并且没有报告任何问题。

任何指点都值得赞赏。

答案1

如果系统日志显示该作业正在被调用,那么可能是路径问题。

尝试将整个路径放在/usr/bin/screen脚本中而不是仅仅screen命令中。

#!/bin/bash
# cd /home/<username>/jupyternb
 /usr/bin/screen -S jupyter -dm jupyter notebook

您还需要在 crontab 中的每一行后都有行尾,如果那是最后一行并且没有行尾字符,则作业将不会启动。

尝试使用mail命令检查 root 用户的系统邮件,或者MAILTO=在 cron 中使用以获取电子邮件,如果在运行 cron 时出现故障,则可能会有更多详细信息。

答案2

我通过删除解决了这个问题计划任务从等式中。https://forums.fast.ai/t/run-jupyter-notebook-on-system-boot/749/5页面很有帮助。

以下是我必须在 /etc/systemd/system 创建的 jupyter.service 文件。

[Unit]
Description=Jupyter Workplace

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=<path_to>/jupyter-notebook --config=<path_to>/jupyter_notebook_config.py
User=<username>
Group=<groupname>
WorkingDirectory=<start up path where notebooks are>
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

然后运行以下命令:

systemctl enable jupyter.service
systemctl daemon-reload
systemctl restart jupyter.service

谢谢大家。

相关内容