1

I wish to use cron to lock the screen at a specific time each day. I have a very simple script containing the following: gnome-screensaver-command -l (locks the screen). I know that this program works because I have run it manually through the terminal and was met with success, however, cron appears to be unable to lock the screen. I am using Ubuntu 16.04 LTS if that helps. My crontab is below.

MALTO=arctic_hen7
SHELL=/bin/bash
25 7 * * * export DISPLAY=:0 && /home/arctic_hen7/bin/lockscreen

I have tried restarting cron and also running the command directly through cron (rather than running the file) and neither approach has worked. I've tried changing the time that the task executes at and waiting for the next minute, but it never executes, however, when I run service cron status, I get this:

(arctic_hen7) RELOAD (crontabs/arctic_hen7)
pam_unix(cron:session): session opened for user arctic_hen7 by (uid=0)
(arctic_hen7) CMD (export DISPLAY=:0 && /home/arctic_hen7/bin/lockscreen)
(CRON) info (No MTA installed, discarding output)

I would like to know if I am doing something wrong, or if there is another way to get cron to lock the screen. I am open to suggestions regarding alternate scheduling applications if it comes to that, however answers regarding cron would be preferable.

6
  • (1) Are you sure that your script is even running at all?  Try putting a command like date >> /tmp/lockscreen.log in it.  (2) Better yet, capture the stdout, stderr, and exit status ($?), and log them.  (3) Something else to try: make a copy or your script (e.g., call it lockscreen0) and put the export DISPLAY=:0 command into that script. Sep 21, 2017 at 0:24
  • I don't know if this exists in your Ubuntu variant, but in mine (Kubuntu), the "Alarm" thing can also run commands at predetermined times. This may be a better choice (the locker process being run with the environment of the graphical desktop).
    – xenoid
    Sep 21, 2017 at 7:59
  • Can you please post the /home/arctic_hen7/bin/lockscreen file? gnome-screensaver-command is probably not in the cron PATH
    – pim
    Sep 21, 2017 at 14:05
  • Possibly related: Adjust brightness with xrandr and cron job (on Ask Ubuntu). Sep 22, 2017 at 17:49
  • Possibly related: Open a window on a remote X display (why “Cannot open display”)?   (on Unix & Linux) Sep 23, 2017 at 22:20

1 Answer 1

0

The first thing you need to do is to install some sort of MTA (mail transport agent) so you can get the output of your cron command emailed to you. As things currently stand, the key diagnostic information is being discarded so you're going to struggle to fix things. I'd recommend mssmtp as a simple starting point.

Instructions for setting up mssmtp copied from here to prevent link rot:

To begin, we need to install 3 packages:

sudo apt-get install msmtp msmtp-mta ca-certificates

Once these are installed, a default config is required. By default msmtp will look at /etc/msmtprc, so I created that using vim, though any text editor will do the trick. This file looked something like this:

# Set defaults.
defaults
# Enable or disable TLS/SSL encryption.
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Setup WP account's settings.
account <MSMTP_ACCOUNT_NAME>
host smtp.gmail.com
port 587
auth login
user <EMAIL_USERNAME>
password <PASSWORD>
from <FROM_ADDRESS>
logfile /var/log/msmtp/msmtp.log

account default : <MSMTP_ACCOUNT_NAME>

Any of the uppercase items (i.e. <PASSWORD>) are things that need replacing specific to your configuration. The exception to that is the log file, which can of course be placed wherever you wish to log any msmtp activity/warnings/errors to.

Once that file is saved, we’ll update the permissions on the above configuration file — msmtp won’t run if the permissions on that file are too open — and create the directory for the log file.

sudo mkdir /var/log/msmtp
sudo chown -R www-data:adm /var/log/msmtp
sudo chmod 0600 /etc/msmtprc

Next I chose to configure logrotate for the msmtp logs, to make sure that the log files don’t get too large as well as keeping the log directory a little tidier. To do this, we create /etc/logrotate.d/msmtp and configure it with the following file. Note that this is optional, you may choose to not do this, or you may choose to configure the logs differently.

/var/log/msmtp/*.log {
rotate 12
monthly
compress
missingok
notifempty
}

Now that the logging is configured, we need to tell PHP to use msmtp by editing /etc/php/7.0/apache2/php.ini and updating the sendmail path from

sendmail_path =

to

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a <MSMTP_ACCOUNT_NAME> -t"

Here I did run into an issue where even though I specified the account name it wasn’t sending emails correctly when I tested it. This is why the line account default : was placed at the end of the msmtp configuration file. To test the configuration, ensure that the PHP file has been saved and run sudo service apache2 restart, then run php -a and execute the following

mail ('[email protected]', 'Test Subject', 'Test body text');
exit();

Any errors that occur at this point will be displayed in the output so should make diagnosing any errors after the test relatively easy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .