Ash*_*ani 4 python scripts cron desktop-environments mate
我是 linux 新手,我阅读并遵循了许多说明在重新启动时运行 python 脚本,但它们都不起作用:
这是我要运行的命令:
/usr/bin/python3 /home/am/projects/AMAcc/map.py
Run Code Online (Sandbox Code Playgroud)
我使用crontab -e并添加了
@reboot /usr/bin/python3 /home/am/projects/AMAcc/map.py
Run Code Online (Sandbox Code Playgroud)
我尝试sudo crontab -e并添加了相同的命令
我什至编辑了/etc/crontab文件并添加了上面的命令。
尝试#!/usr/bin/python3在我的 python 代码顶部添加并更改其权限,例如:
sudo chmod a+x map.py
正如文件中建议所有用户都将能够如果运行cron作业cron.deny文件存在,但其空的,所以我试图创建空的cron.deny两个文件/etc/cron.deny和/usr/lib/cron/cron.deny。但是仍然没有运气运行 cron 工作。
我在做什么?
要从 Cron 执行任何 GUI 应用程序,您应该导出一些桌面环境变量。大多数情况下,导出当前$DISPLAY值就足够了。我建议您创建一个额外的启动 (bash) 脚本,它将启动您的应用程序。
在这个答案中提供了如何找到和导出当前用户的$DISPLAY价值的可靠方法。但有一个问题。当用户未登录到 DE 时,该$DISPLAY值未设置。? 我们可以添加一个额外的条件,就像在这个答案中所做的那样:
# Get the value of the $DISPLAY variable for the current user. Unset it just in case this is a `ssh -X` connection
unset DISPLAY;
while [ "$DISPLAY" == "" ]; do
export DISPLAY=$(w "$USER" | awk 'NF > 7 && $2 ~ /tty[0-9]+/ {print $3; exit}' 2>/dev/null) && sleep 3
done
Run Code Online (Sandbox Code Playgroud)
@rebootCron 作业中失败,我无法弄清楚原因。因此,在下面的脚本中使用了另一种方法来检查用户是否已登录 Mate。一些应用程序需要更多的变量(例如,参见这个和这个答案)作为$DBUS_SESSION_BUS_ADDRESS和$DESKTOP_SESSION。在我看来,最好的办法是找到当前用户的桌面会话进程在目录中/proc(过程信息伪文件系统),并从文件中导出所有的变量environ是内/proc/process-number。
我专门为此创建了一个名为cron-gui-launcher的小项目。它有效,但并未针对所有用户的情况(如当前)进行测试。这里我提取了提到Mate DE的必要部分,并进行了上面提到的修改。
1.使用以下内容创建一个可执行文件:
#!/bin/bash -e
# Check whether the user is logged in Mate
while [ -z "$(pgrep mate-session -n -U $UID)" ]; do
sleep 3 && count=$((count+1)) && echo "$count" > /tmp/mate-cron.log
done
# Get the content of the Current-Desktop-Session Environment File as an array:
EnvVarList=`cat -e "/proc/$(pgrep mate-session -n -U $UID)/environ" | sed 's/\^@/\n/g'`
# Export the Current-Desktop-Session Environment Variables:
for EnvVar in $EnvVarList; do
echo "$EnvVar" >> /tmp/mate-cron.log
export "$EnvVar"
done
# Execute the list of the input commands
nohup "${1}" >/dev/null 2>&1 &
exit 0
Run Code Online (Sandbox Code Playgroud)
2.正在crontab使用中:
@reboot /path-to-the/script "your-application"
Run Code Online (Sandbox Code Playgroud)
3.注意事项:
此脚本不适用于 root 的crontab. 由于变量$UID和$USER. 您可以用特定用户的 UID 和 NAME 替换它们。
对于其他 DEmate-session从这部分更改为其他 DE$(pgrep mate-session -n -U $UID)的进程名称,例如gnome-session。
也许对于在系统启动时执行的 GUI 应用程序,最好在 Startup Applications 中创建一个条目。对于伴侣DE: Control Centre > Personal > Startup Applications(源)。
4.这里展示了脚本是如何工作的:
| 归档时间: |
|
| 查看次数: |
3225 次 |
| 最近记录: |