Score:0

Trying to run a script as a daemon on reboot using CRON in Ubuntu 22.04

us flag

As a workaround to the known bug in 22.04 LTS which does not turn off screen backlighting on inactivity, I found the following script:

#!/bin/sh  
#Screen timeout script screen-lock-5-min.sh
#
  
#Wanted trigger timeout in milliseconds.  
IDLE_TIME=$((5*60*1000))  # replace the '5' with how many minutes you'd like  
  
echo "Screen lock active."  
  
#Sequence to execute when timeout triggers.  
trigger_cmd() {  
    echo "Triggered action $(date)"  
}  
  
sleep_time=$IDLE_TIME  
triggered=false  
  
while sleep $(((sleep_time+999)/1000)); do  
    idle=$(xprintidle)  
    if [ $idle -ge $IDLE_TIME ]; then  
        if ! $triggered; then  
            # gnome-screensaver-command -l  
            export DISPLAY=:0; xset dpms force off  
            triggered=true  
            sleep_time=$IDLE_TIME  
        fi  
    else  
        triggered=false  
        # Give 150 ms buffer to avoid frantic loops shortly before triggers.  
        sleep_time=$((IDLE_TIME-idle+150))  
    fi  
done  

This works beautifully: when I run it in Terminal, the screen timeout properly turns off the screen after the specified time, as long as I kept the terminal session open. Closing the Terminal stops the process, disabling the timeout feature. However, I found I could keep the script running as a daemon by invoking it as:

nohup /home/username/screen-lock-5-min.sh &  

I could then close Terminal and the script would continue to run in the background.

Now for the hard part: I want to run this script on reboot. I added the following line to my crontab file:

@reboot nohup /home/username/screen-lock-5-min.sh &  

(I had previously tried invoking the script in CRON without nohup and it did not work.) I can see that the script is running on reboot because nohup appends the "screen lock is active" message to the nohup.out file. But for some reason the process is stopped at some point: the script does not blank the screen; I still have to manually run the script in Terminal on startup. I cannot for the life of me figure out how to keep this script running in the background.

Any help would be appreciated.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.