Cron 作业 运行 可以作为脚本但不是@reboot
Cron jobs run ok as script but not @reboot
我是 运行 无外设远程 Ubuntu (v20.04.3) 上的 Django (v3.1.7) 应用程序,我希望它在使用 crontab 重新启动时启动。下面的脚本从命令行运行一切正常。我也浏览过这里 crontabs-reboot-only-works-for-root 和链接的建议,但仍未确定问题所在。我添加了日志记录(请参阅下面的 *.log
)但没有得到任何有用的输出。
编辑: test
屏幕在命令行和重启时都有效。 django
和 streaming
屏幕在重新启动时不起作用。使用 which python3
returns /usr/bin/python3
检查 python 可执行文件。所以在我看来,我对 reboot.sh
具有正确的权限,并且 cron
应该能够找到 python3
,因为它在 crontab
文件的 PATH=
中.
/home/myapp/reboot.sh
#!/bin/bash
echo "$(date) Starting reboot.sh"
start_django () {
echo "$(date) Entering start_django"
screen -S django -dm bash -c 'cd /home/myapp && /usr/local/bin/gunicorn myapp.wsgi:application --bind 0.0.0.0:8000' > /home/myapp/django_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_django"
}
start_stream () {
echo "$(date) Entering start_stream"
screen -S streaming -dm bash -c 'cd /home/myapp/data_api && python3 api_stream_client.py' > /home/myapp/stream_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_stream"
}
start_test () {
echo "$(date) Entering start_test"
screen -S testa -dm bash -c 'cd /home/myapp && exec bash' > /home/myapp/test_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_test"
}
if screen -list | grep -q "No Sockets found"; then
echo "$(date) No screens available"
echo "$(date) Starting test"
start_test
echo "$(date) Starting django"
start_django
echo "$(date) Starting stream"
start_stream
else
if screen -list | grep -q "django"; then
echo "$(date) django exists"
else
echo "$(date) django does not exist - starting now"
start_django
fi
if screen -list | grep -q "streaming"; then
echo "$(date) stream exists"
else
echo "$(date) stream does not exist - starting now"
start_stream
fi
if screen -list | grep -q "test"; then
echo "$(date) test exists"
else
echo "$(date) test does not exist - starting now"
start_test
fi
fi
contab -l
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
@reboot sleep 60 && /home/myapp/reboot.sh > /home/myapp/cron.log 2>&1
/var/log/cron.log
Jan 28 15:21:44 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:44 localhost cron[814]: (CRON) INFO (pidfile fd = 3)
Jan 28 15:21:44 localhost cron[814]: (CRON) INFO (Running @reboot jobs)
Jan 28 15:21:44 localhost CRON[824]: (root) CMD (sleep 60 && /home/financialdashboard/reboot.sh > /home/financialdashboard/cron.log 2>&1)
Jan 28 15:21:46 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:50 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:55 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:55 localhost systemd-timesyncd[637]: Initial synchronization to time server [2001:67c:1560:8003::c7]:123 (ntp.ubuntu.com).
/home/myapp/cron.log
Fri Jan 28 15:22:44 UTC 2022 Starting reboot.sh
Fri Jan 28 15:22:44 UTC 2022 No screens available
Fri Jan 28 15:22:44 UTC 2022 Starting test
Fri Jan 28 15:22:44 UTC 2022 Entering start_test
Fri Jan 28 15:22:45 UTC 2022 Exiting start_test
Fri Jan 28 15:22:45 UTC 2022 Starting django
Fri Jan 28 15:22:45 UTC 2022 Entering start_django
Fri Jan 28 15:22:46 UTC 2022 Exiting start_django
Fri Jan 28 15:22:46 UTC 2022 Starting stream
Fri Jan 28 15:22:46 UTC 2022 Entering start_stream
Fri Jan 28 15:22:47 UTC 2022 Exiting start_stream
django_cron.log
、stream_cron.log
和test_cron.log
都是空的
建议修复您的 crontab -l
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
@reboot /usr/bin/sleep 60 && /home/myapp/reboot.sh > /home/myapp/cron.log 2>&1
建议修复您的 /home/myapp/reboot.sh
在第 2 行插入 source /home/myapp/.bash_profile
。
sed -i "2i source /home/myapp/.bash_profile" /home/myapp/reboot.sh
已修复:django
和 streaming
屏幕依赖于某些未加载 cron
的环境变量。有关详细信息,请参阅此处 where-can-i-set-environment-variables-that-crontab-will-use。
按照此 gnu-screen-logtstamp-string and this save-screen-program-output-to-a-file 我能够在屏幕上进行日志记录。
我更新了 /home/myapp/reboot.sh:
#!/bin/bash
SHELL=/bin/bash
echo "$(date) Starting reboot.sh"
# Load environment variables
source /etc/profile.d/env.sh
start_django () {
echo "$(date) Entering start_django"
screen -c /home/myapp/django_cron_log.conf -L -S django -dm bash -c 'cd /home/myapp && /usr/local/bin/gunicorn myapp.wsgi:application --bind 0.0.0.0:8000'
echo "$(date) Exiting start_django"
}
start_stream () {
echo "$(date) Entering start_stream"
screen -c /home/myapp/stream_cron_log.conf -L -S streaming -dm bash -c 'cd /home/myapp/data_api && python3 api_stream_client.py && exec bash'
echo "$(date) Exiting start_stream"
}
if screen -list | grep -q "No Sockets found"; then
echo "$(date) No screens available"
echo "$(date) Starting django"
start_django
echo "$(date) Starting stream"
start_stream
else
if screen -list | grep -q "django"; then
echo "$(date) django exists; not starting django"
else
echo "$(date) django does not exist - starting now"
start_django
fi
if screen -list | grep -q "streaming"; then
echo "$(date) stream exists; not starting stream"
else
echo "$(date) stream does not exist - starting now"
start_stream
fi
fi
screen
中的 -c /home/myapp/*_cron_log.conf -L
将被删除用于生产。
我是 运行 无外设远程 Ubuntu (v20.04.3) 上的 Django (v3.1.7) 应用程序,我希望它在使用 crontab 重新启动时启动。下面的脚本从命令行运行一切正常。我也浏览过这里 crontabs-reboot-only-works-for-root 和链接的建议,但仍未确定问题所在。我添加了日志记录(请参阅下面的 *.log
)但没有得到任何有用的输出。
编辑: test
屏幕在命令行和重启时都有效。 django
和 streaming
屏幕在重新启动时不起作用。使用 which python3
returns /usr/bin/python3
检查 python 可执行文件。所以在我看来,我对 reboot.sh
具有正确的权限,并且 cron
应该能够找到 python3
,因为它在 crontab
文件的 PATH=
中.
/home/myapp/reboot.sh
#!/bin/bash
echo "$(date) Starting reboot.sh"
start_django () {
echo "$(date) Entering start_django"
screen -S django -dm bash -c 'cd /home/myapp && /usr/local/bin/gunicorn myapp.wsgi:application --bind 0.0.0.0:8000' > /home/myapp/django_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_django"
}
start_stream () {
echo "$(date) Entering start_stream"
screen -S streaming -dm bash -c 'cd /home/myapp/data_api && python3 api_stream_client.py' > /home/myapp/stream_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_stream"
}
start_test () {
echo "$(date) Entering start_test"
screen -S testa -dm bash -c 'cd /home/myapp && exec bash' > /home/myapp/test_cron.log 2>&1
sleep 1
echo "$(date) Exiting start_test"
}
if screen -list | grep -q "No Sockets found"; then
echo "$(date) No screens available"
echo "$(date) Starting test"
start_test
echo "$(date) Starting django"
start_django
echo "$(date) Starting stream"
start_stream
else
if screen -list | grep -q "django"; then
echo "$(date) django exists"
else
echo "$(date) django does not exist - starting now"
start_django
fi
if screen -list | grep -q "streaming"; then
echo "$(date) stream exists"
else
echo "$(date) stream does not exist - starting now"
start_stream
fi
if screen -list | grep -q "test"; then
echo "$(date) test exists"
else
echo "$(date) test does not exist - starting now"
start_test
fi
fi
contab -l
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
@reboot sleep 60 && /home/myapp/reboot.sh > /home/myapp/cron.log 2>&1
/var/log/cron.log
Jan 28 15:21:44 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:44 localhost cron[814]: (CRON) INFO (pidfile fd = 3)
Jan 28 15:21:44 localhost cron[814]: (CRON) INFO (Running @reboot jobs)
Jan 28 15:21:44 localhost CRON[824]: (root) CMD (sleep 60 && /home/financialdashboard/reboot.sh > /home/financialdashboard/cron.log 2>&1)
Jan 28 15:21:46 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:50 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:55 localhost systemd-timesyncd[637]: Network configuration changed, trying to establish connection.
Jan 28 15:21:55 localhost systemd-timesyncd[637]: Initial synchronization to time server [2001:67c:1560:8003::c7]:123 (ntp.ubuntu.com).
/home/myapp/cron.log
Fri Jan 28 15:22:44 UTC 2022 Starting reboot.sh
Fri Jan 28 15:22:44 UTC 2022 No screens available
Fri Jan 28 15:22:44 UTC 2022 Starting test
Fri Jan 28 15:22:44 UTC 2022 Entering start_test
Fri Jan 28 15:22:45 UTC 2022 Exiting start_test
Fri Jan 28 15:22:45 UTC 2022 Starting django
Fri Jan 28 15:22:45 UTC 2022 Entering start_django
Fri Jan 28 15:22:46 UTC 2022 Exiting start_django
Fri Jan 28 15:22:46 UTC 2022 Starting stream
Fri Jan 28 15:22:46 UTC 2022 Entering start_stream
Fri Jan 28 15:22:47 UTC 2022 Exiting start_stream
django_cron.log
、stream_cron.log
和test_cron.log
都是空的
建议修复您的 crontab -l
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
@reboot /usr/bin/sleep 60 && /home/myapp/reboot.sh > /home/myapp/cron.log 2>&1
建议修复您的 /home/myapp/reboot.sh
在第 2 行插入 source /home/myapp/.bash_profile
。
sed -i "2i source /home/myapp/.bash_profile" /home/myapp/reboot.sh
已修复:django
和 streaming
屏幕依赖于某些未加载 cron
的环境变量。有关详细信息,请参阅此处 where-can-i-set-environment-variables-that-crontab-will-use。
按照此 gnu-screen-logtstamp-string and this save-screen-program-output-to-a-file 我能够在屏幕上进行日志记录。
我更新了 /home/myapp/reboot.sh:
#!/bin/bash
SHELL=/bin/bash
echo "$(date) Starting reboot.sh"
# Load environment variables
source /etc/profile.d/env.sh
start_django () {
echo "$(date) Entering start_django"
screen -c /home/myapp/django_cron_log.conf -L -S django -dm bash -c 'cd /home/myapp && /usr/local/bin/gunicorn myapp.wsgi:application --bind 0.0.0.0:8000'
echo "$(date) Exiting start_django"
}
start_stream () {
echo "$(date) Entering start_stream"
screen -c /home/myapp/stream_cron_log.conf -L -S streaming -dm bash -c 'cd /home/myapp/data_api && python3 api_stream_client.py && exec bash'
echo "$(date) Exiting start_stream"
}
if screen -list | grep -q "No Sockets found"; then
echo "$(date) No screens available"
echo "$(date) Starting django"
start_django
echo "$(date) Starting stream"
start_stream
else
if screen -list | grep -q "django"; then
echo "$(date) django exists; not starting django"
else
echo "$(date) django does not exist - starting now"
start_django
fi
if screen -list | grep -q "streaming"; then
echo "$(date) stream exists; not starting stream"
else
echo "$(date) stream does not exist - starting now"
start_stream
fi
fi
screen
中的 -c /home/myapp/*_cron_log.conf -L
将被删除用于生产。