在 rc.local 中执行的 Python 脚本不会记录,除非使用非零代码

在 rc.local 中执行的 Python 脚本不会记录,除非使用非零代码

我的rc.local服务在启动时执行一个 python 脚本:它工作正常,但不会在任何地方记录,除非发生错误并以非零代码退出。该脚本运行计划的操作,因此它将在后台挂起运行,除非发生错误或手动停止。

这就是我的 rc.local 的样子:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


cd /root/path/to/script
/root/path/to/script/venv/bin/python /root/path/to/script/main.py &
exit 0

我检查了syslog文件,cat /var/log/syslog | grep rc.local但它仅在线程启动或停止时记录(与 的信息相同systemctl status rc-local.service):

Mar 28 12:49:07 machine-hostname systemd[1]: Starting /etc/rc.local Compatibility...
Mar 28 12:49:07 machine-hostname systemd[1]: Started /etc/rc.local Compatibility.

我尝试通过覆盖 python 脚本将输出重定向到外部文件stdout,但它不起作用。我使用虚拟环境来避免安装各种所需的 python 依赖项(例如requestsschedule),这可能是问题的原因吗?我是 python 和 linux 世界的新手,所以也许我犯了一些配置错误

PS:我需要移入脚本执行文件夹,因为 python 脚本openvpn在内部运行命令,并且它需要脚本执行文件夹中的配置文件。以下是systemctl status rc-local.service命令输出的示例:

● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (running) since Sun 2021-03-28 12:49:07 CEST; 1h 32min ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 10033 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/rc-local.service
           ├─10038 /root/path/to/script/venv/bin/python /root/path/to/script/main.py
           └─10068 openvpn configuration-file.ovpn

mar 28 12:49:07 machine-hostname systemd[1]: Starting /etc/rc.local Compatibility...
mar 28 12:49:07 machine-hostname systemd[1]: Started /etc/rc.local Compatibility.

这是一个脚本片段:

import requests
import os
import subprocess
import shutil
import fileinput
import schedule
import time
from urllib.request import urlopen


# global variables declaration
...

# functions declaration
...


if __name__ == '__main__':
    print('Starting VPN connection...')
    server_list = get_server_list(max_servers_limit) # calls a public api
    connect(server_list) # executes openvpn command

    print('Scheduling VPN connection change every {} hours'.format(change_vpn_every_hours))
    schedule.every(change_vpn_every_hours).hours.do(change_vpn_connection)

    print('Scheduling internet connection checking every {} minutes'.format(check_internet_connection_minutes))
    schedule.every(check_internet_connection_minutes).minutes.do(check_internet_connection)
    while True:
        schedule.run_pending()
        time.sleep(1)

更新:它似乎几乎“随机”或在某些情况下记录,例如当它更改 VPN 连接时,而不仅仅是当它结束执行返回退出代码时...这是journalctl -u rc-local.service命令的输出(我的注释以“#”开头##”来解释到底发生了什么):

-- Reboot --
mar 28 20:59:27 machine-hostname systemd[1]: Starting /etc/rc.local Compatibility...
mar 28 20:59:27 machine-hostname systemd[1]: Started /etc/rc.local Compatibility.
mar 29 03:05:14 machine-hostname rc.local[1063]: Starting VPN connection: connecting to [...]   ### This was happened at 20:59:27, when the script execution started
mar 29 03:05:14 machine-hostname rc.local[1063]: Searching configuration file of server with hostname XXX
mar 29 03:05:14 machine-hostname rc.local[1063]: Found server with hostname XXX
mar 29 03:05:14 machine-hostname rc.local[1063]: IP address: XXX
mar 29 03:05:14 machine-hostname rc.local[1063]: Load: 23
mar 29 03:05:14 machine-hostname rc.local[1063]: Country: Italy, City: Milan
mar 29 03:05:14 machine-hostname rc.local[1063]: Latitude: XX.X, Longitude: X.X
mar 29 03:05:14 machine-hostname rc.local[1063]: Launching openvpn...
mar 29 03:05:14 machine-hostname rc.local[1063]: ###### [COMMAND OUTPUT] ######
mar 29 03:05:14 machine-hostname rc.local[1063]: Sun Mar 28 20:59:28 2021 OpenVPN 2.4.4 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [A
mar 29 03:05:14 machine-hostname rc.local[1063]: Sun Mar 28 20:59:28 2021 library versions: OpenSSL 1.1.1  11 Sep 2018, LZO 2.08
mar 29 03:05:14 machine-hostname rc.local[1063]: [...] 
mar 29 03:05:14 machine-hostname rc.local[1063]: Sun Mar 28 20:59:31 2021 Initialization Sequence Completed
mar 29 03:05:14 machine-hostname rc.local[1063]: ##############################
mar 29 03:05:14 machine-hostname rc.local[1063]: Connected successfully
mar 29 03:05:14 machine-hostname rc.local[1063]: Pid: 1592
mar 29 03:05:14 machine-hostname rc.local[1063]: Scheduling VPN connection change every 12 hours
mar 29 03:05:14 machine-hostname rc.local[1063]: Scheduling internet connection checking every 5 minutes     ### The two schedulers have started
mar 29 03:05:14 machine-hostname rc.local[1063]: Checking internet connection ### This was happened at 21:05, 5 minutes after the execution
mar 29 03:05:14 machine-hostname rc.local[1063]: Internet is up
mar 29 03:05:14 machine-hostname rc.local[1063]: [...]    ### It goes on this way for 12 hours: no connection problem occurred so it hasn't restarted the connection and changed VPN...
mar 29 09:05:57 machine-hostname rc.local[1063]: Checking internet connection
mar 29 09:05:57 machine-hostname rc.local[1063]: Internet is up
mar 29 09:05:57 machine-hostname rc.local[1063]: [SCHEDULER]: Changing VPN connection    ### First change vpn scheduler execution: it really happened at 09:05
mar 29 09:05:57 machine-hostname rc.local[1063]: [SCHEDULER]: Killing process 1592
mar 29 09:05:57 machine-hostname rc.local[1063]: [SCHEDULER]: Restarting network manager
mar 29 09:05:57 machine-hostname rc.local[1063]: [SCHEDULER]: Retrieving server list
mar 29 09:05:57 machine-hostname rc.local[1063]: [SCHEDULER]: Connecting to new server
mar 29 09:05:57 machine-hostname rc.local[1063]: Searching configuration file of server with hostname XXX
mar 29 09:05:57 machine-hostname rc.local[1063]: Found server with hostname XXX
mar 29 09:05:57 machine-hostname rc.local[1063]: [...]    ### Same stuff as before...
mar 29 09:05:57 machine-hostname rc.local[1063]: Connected successfully
mar 29 09:05:57 machine-hostname rc.local[1063]: Pid: 27188
mar 29 09:05:57 machine-hostname rc.local[1063]: Checking internet connection
mar 29 09:05:57 machine-hostname rc.local[1063]: Internet is up
mar 29 09:05:57 machine-hostname rc.local[1063]: Checking internet connection

我说“随机”是因为日志中的时间在 03:05 发生变化(当没有什么特殊情况发生时,它检查互联网是否已启动...)和 09:05,当它更改 VPN 连接时,顺便说一句,它已经几分钟前刚刚记录,再次触发 vpn 更改连接调度程序时...我认为这种明显的随机日志记录可能是由time.sleep()python 脚本中的函数调用引起的(它也在更改 vpn 连接的方法中调用),但我一点也不确定。

答案1

您是否尝试过像这样手动将输出传输到日志文件中: python3 main.py 1> output.log 2> errors.log或者只是python3 main.py > everything.log (您需要找到调用脚本来执行此操作的文件)

阅读完 python 代码后,print()输出到 stdout (不是日志!),除非有一些 rc.local 的东西我不知道如何将 stdout 保存在日志中

答案2

我会尝试删除&.我认为 systemd 会为你把它放在后台。另外,如果它在后台,则将exit 0运行并且 shell 将退出,除非您使用了nohup.这有帮助吗?

相关内容