Ubuntu: Login programmatically after waking up



Question:

My scenario is that I wish to use rtcwake -m no to wake up my computer while I'm asleep and use systemd/systemd-suspend scripts to trigger smplayer to wake me up with a queue of podcasts.

My difficulty is that smplayer can't run on wake up because Ubuntu loads the login screen and is waiting for a login. Is there a way that I can login programmatically? If not, can I somehow run a GUI program such as smplayer as root user while the login screen is displayed? (So far, it's not working out that way.)

(I don't wish to suspend the computer to a simple sleep, thereby never logging out, because I don't want to have to know in advance when I suspend/sleep my computer that I'm doing so for the last time of the day. To be clear, I don't want to automatically login in all cases, only when the computer is awakened for this purpose.)

I'm on LTS 16.


Solution:1

You can use xdotool to login, but there are a few things to keep in mind:

  1. In order to have the login screen's window selected for xdotool, you need to set the XAUTHORITY variable to the .Xauthority file that corresponds to your user (usually /home/myuser/.Xauthority. Also set the DISPLAY variable. See code sample below.
  2. Run xdotool as the user who is to login. (Your script will run as root. Actually it will run as your logged-in user under some circumstances, such as causing sleep by /lib/systemd/systemd-sleep suspend, but under ordinary circumstances, i.e. systemctl suspend, it will run as root.)
  3. To make this only fire when your alarm is the cause of your wakeup, make use of the linux/rtc.h API to compare the machine's current time (RTC_RD_TIME) to the alarm time (RTC_ALM_READ) ON /dev/rtc0. If they are near together when your script in systemd/system-sleep runs, then you may surmise that this is an alarm-triggered wakeup.

Sample code:

#!/bin/sh  # /lib/systemd/system-sleep/myscript  # PW64 is a variable holding a base64-encoded password    if [ post = $1 ] && [ suspend = $2 ] && [ $(whoami) = root ]; then    if get_alm_time 10; then # this is an external program you should write to compare RTC_RD_TIME TO RTC_ALM_TIME      export XAUTHORITY=/home/mysuer/.Xauthority      export DISPLAY=:0      su -c "xdotool type $(printf $PW64 | base64 -d)" myuser &&\      su -c 'xdotool key Return' myuser &&\      su -c 'bash my-other-script.sh' myuser    fi  fi  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »