deepin系统 crontab 执行自动任务

deepin 中的锁屏功能

dde-lock命令

crontab功能

设置按固定的周期(如每天、每周等)重复执行预先计划好的任务。

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |# *  *  *  *  * user-name command to be executed
注意:今后写脚本让crontab去自动执行的时候,尽量写命令的绝对目录。

管理cron计划任务

  root用户可以管理指定用户的计划任务、普通用户只能管理自己的计划任务

  1、编辑计划任务:crontab  -e  -u  用户名

  2、查看计划任务:crontab  -l  -u  用户名

  3、删除计划任务:crontab  -r  -u  用户名

在deepin系统中实现锁屏

借鉴:https://selivan.github.io/2016/07/08/notify-send-from-cron-in-ubuntu.html

  1. 创建/usr/local/bin/gui-program-from-cron.sh文件
#!/bin/sh
[ "$#" -lt 1 ] && echo "Usage: $0 program options" && exit 1

program="$1"
shift

user=$(whoami)
env_reference_process=$( pgrep -u "$user" xfce4-session || pgrep -u "$user" dde-desktop || pgrep -u "$user" ciannamon-session || pgrep -u "$user" gnome-session || pgrep -u "$user" gnome-shell || pgrep -u "$user" kdeinit | head -1 )

export DBUS_SESSION_BUS_ADDRESS=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DBUS_SESSION_BUS_ADDRESS= | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
export DISPLAY=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DISPLAY= | sed 's/DISPLAY=//')
"$program" "$@"

  1. 编写crontab
*/30 8-18 * * * /usr/local/bin/gui-program-from-cron.sh /usr/bin/dde-lock

猜你喜欢

转载自blog.csdn.net/lxmuyu/article/details/115403936