Quantcast
Channel: Question and Answer » cron
Viewing all articles
Browse latest Browse all 165

How to make cron job which suspends computer before specified time of the day

$
0
0

I am trying to make script which suspends computer if it is used before specified time of the day or if it is used too long.

#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0
day=$(date +%u)
if (( day == 1 || day == 3 || day == 5 )) ; then
time=1830
elif  (( day == 2 || day == 4 )) ; then
time=2030
elif (( day == 6 || day == 7 )) ; then
time=2000
fi


if [ $(date +%H%M) -lt $time ] ; then
xmessage -nearmouse "last_warning"
sleep 300
i3lock && systemctl suspend
else
sleep 9000
xmessage -nearmouse "warning"
sleep 1500
xmessage -nearmouse "last_warning"
sleep 300
i3lock && systemctl suspend
fi

It was working fine but I needed to make it “indestructible” by simply killing the process. so I made second script which should check if the first one is running and if not, execute it.

#!/bin/bash  
process=TControl.sh
makerun="/home/roman/.Scripts/TControl.sh"

if ps ax | grep -v grep | grep $process > /dev/null
then
  exit
else
  $makerun >> /home/roman/.Scripts/Tcontrol.Err 2>&1
  sleep 2
fi

Then I made crontab entry to run the second script every minute. But problem was that although the first script started it did not show any of these xmessages or suspended computer. So I googled here and here and found that it might be problem with environment variables which are different for cron. Because of that I added those two lines after the heading of the first script.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0

It did not help except that now when I redirect Stdout and Stderr of the first script it writes:

No protocol specified
Error: Can't open display: :0

Can anyone explain me where is the problem? Should I put these env-variables somewhere else?


Viewing all articles
Browse latest Browse all 165

Latest Images

Trending Articles



Latest Images