如何在linux启动时自动启动自动python程序/脚本?

如何在linux启动时自动启动自动python程序/脚本?,python,linux,selenium,systemd,Python,Linux,Selenium,Systemd,每次linux启动时,我都试图执行一个python程序,它使用selenium登录一个页面,获取一些信息,并使用notify send将其显示为通知 注:我正在使用软呢帽 硒用作无头电池: options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage')

每次linux启动时,我都试图执行一个python程序,它使用selenium登录一个页面,获取一些信息,并使用
notify send
将其显示为通知

注:我正在使用软呢帽

硒用作无头电池:

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=options)
...
...
info = f" 'You consumed {consumedQuota} Gb, remaining{remainingQuota} Gb and there are still 
{remainingDays} days!' \n"

ex = 'notify-send -u critical ' + 'WE-Indicator' + info
print(ex)
os.system(str(ex))
运行服务(不是在启动时)可以工作,但不会显示任何通知:

● internet_quote.service - WE-Internet
  Loaded: loaded (/etc/systemd/system/internet_quote.service; enabled; vendor preset: disabled)
  Active: inactive (dead) since Mon 2020-06-22 20:47:24 EET; 15s ago
  Process: 4735 ExecStart=/usr/local/bin/script.sh (code=exited, status=0/SUCCESS)
  Main PID: 4735 (code=exited, status=0/SUCCESS)
    CPU: 7.084s

Jun 22 20:47:07 192.168.1.6 systemd[1]: Started WE-Internet.
Jun 22 20:47:24 192.168.1.6 script.sh[4746]: notify-send -u critical WE-Indicator 'You consumed 29.96 Gb, remaining 110.04 Gb and there are still 22  days!'
Jun 22 20:47:24 192.168.1.6 systemd[1]: internet_quote.service: Succeeded.
Jun 22 20:47:24 192.168.1.6 systemd[1]: internet_quote.service: Consumed 7.084s CPU time.
在启动时记录:

● internet_quote.service - WE-Internet
  Loaded: loaded (/etc/systemd/system/internet_quote.service; enabled; 
  vendor preset: disabled)
  Active: failed (Result: exit-code) since Mon 2020-06-22 19:29:06 EET; 1h 9min ago
  Process: 838 ExecStart=/usr/local/bin/script.sh (code=exited, status=1/FAILURE)
  Main PID: 838 (code=exited, status=1/FAILURE)
    CPU: 592ms

    Jun 22 19:29:06 localhost.localdomain script.sh[942]:     response = self.execute(Command.NEW_SESSION, parameters)
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:   File "/home/none/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:     self.error_handler.check_response(response)
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:   File "/home/none/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:     raise exception_class(message, screen, stacktrace)
    Jun 22 19:29:06 localhost.localdomain script.sh[942]: selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:   (unknown error: DevToolsActivePort file doesn't exist)
    Jun 22 19:29:06 localhost.localdomain script.sh[942]:   (The process started from chrome location /usr/lib64/chromium-browser/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
    Jun 22 19:29:06 localhost.localdomain systemd[1]: internet_quote.service: Main process exited, code=exited, status=1/FAILURE
    Jun 22 19:29:06 localhost.localdomain systemd[1]: internet_quote.service: Failed with result 'exit-code'.
我使用sysemd运行服务:
sudono nano/etc/systemd/system/internet\u quote.service

[Unit]
Description=WE-Internet

[Service]
WorkingDirectory=/home/none/
Environment="DISPLAY=:0" "XAUTHORITY=/home/none/.Xauthority"
User=none
ExecStart=/usr/local/bin/script.sh

[Install]
WantedBy=multi-user.target
这是我在
/usr/local/bin/script.sh中编写的简单脚本:

#!/bin/bash
cd 
cd /home/none/code/WE_Usage
python we.py
更新

我发现home目录中没有
.Xauthority

[none@192 ~]$ echo $XAUTHORITY
/run/user/1000/.mutter-Xwaylandauth.OBCMM0
因此,我尝试将此路径添加到
.service
,但没有成功 根据:,
notify send
不适用于
root
用户

Hello 因此,在logerror“Chrome”出现时,X似乎没有启动 启动失败:出现“异常退出”。也许这就足够了 加上

…在“互联网服务”中


在第一种情况下(不是启动)不起作用,没有通知,这意味着这段代码
os.system(str(ex))
被执行,但
notify send
没有显示任何,这很奇怪。我想你需要为X设置DISPLAY=:0?你是说这个吗
Environment=“DISPLAY=:0”“XAUTHORITY=/home/none/.XAUTHORITY”
否。但我尝试了notify send,但它在我的系统(基于debianbased)上也无法正常工作。我试过这个:消息弹出。i、 e.使用:
zenity--info--text=“这是世界末日!”
[Unit]
After=display-manager.service