I want to run a cron job periodically which locks my pc.
I type any of these 3 commands in the terminal and they work:
dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
xdg-screensaver lock
gnome-screensaver-command -l
When I put them in cron, nothing happens.
I have 2 other jobs that run SSH. They work while this one specifically won't.
I tried adding environments, but that wouldn't work either. This is what I tried to make crontab look like:
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /usr/bin/xdg-screensaver lock
(newline,EOF)
I tried with and without /usr/bin/
prefixes to the commands. I tried with and without environments for cron. I tried SHELL=/bin/bash
as well.
I tried something like nohup xdg-screensaver lock
No results.
I tried putting it in a script, and then running the script via cron. No results.
EDIT:
I tried setting DISPLAY in crontab. When I did echo $DISPLAY
I got :1
in return.
I changed crontab to this:
DISPLAY=:1
* * * * * /usr/bin/xdg-screensaver lock
(newline,EOF)
My PC still won't lock. But now when I check cron's status with service cron status
I see new things. These new lines appear:
├─25234 /usr/sbin/cron -f
├─25852 /usr/sbin/CRON -f
├─25853 /bin/sh -c /usr/bin/xdg-screensaver lock
├─25854 /bin/sh /usr/bin/xdg-screensaver lock
├─25862 dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager
├─25863 dbus-launch --autolaunch 6cd12da16cea4d79bb7367a0f8b8bd3e --binary-syntax --close-stderr
└─25866 dbus-launch --autolaunch 6cd12da16cea4d79bb7367a0f8b8bd3e --binary-syntax --close-stderr
Jan 23 22:38:01 riff CRON[25923]: pam_unix(cron:session): session opened for user riff by (uid=0)
Jan 23 22:38:01 riff CRON[25924]: (riff) CMD (/usr/bin/xdg-screensaver lock)
Jan 23 22:38:01 riff dbus-daemon[25938]: [session uid=1000 pid=25936] AppArmor D-Bus mediation is enabled
Jan 23 22:38:04 riff dbus-daemon[25953]: [session uid=1000 pid=25951] AppArmor D-Bus mediation is enabled
Jan 23 22:38:07 riff dbus-daemon[25959]: [session uid=1000 pid=25957] AppArmor D-Bus mediation is enabled
Jan 23 22:38:10 riff CRON[25923]: (CRON) info (No MTA installed, discarding output)
Jan 23 22:38:10 riff CRON[25923]: pam_unix(cron:session): session closed for user riff
The new lines are those starting with dbus-send
, dbus-launch
and dbus-daemon
I tried setting SHELL=/bin/bash in crontab but that didn't change anything
...
After redirecting the error to a log,
Running xdg-screensaver lock
as a cron job i get the error:
ERROR: Unknown command 'lock'.
Using gnome-screensaver -l
I get the error:
** Message: 23:25:04.831: Failed to get session bus: Could not connect: Connection refused
Using dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
I get the error:
Failed to open connection to "session" message bus: Failed to connect to socket /tmp/dbus-ITbw92LLaf: Connection refused
So I tried running the dbus-send.. command with root's crontab. Now the error is:
Failed to open connection to "session" message bus: /usr/bin/dbus-launch terminated abnormally with the following error: No protocol specified
Autolaunch error: X11 initialization failed.
Error after running gnome-screensaver as root cron job:
** Message: 00:06:01.092: Failed to get session bus: Error spawning command line “dbus-launch --autolaunch=6cd12da16cea4d79bb7367a0f8b8bd3e --binary-syntax --close-stderr”: Child process exited with code 1