Automation script(s) to run website X hours and then turn monitor on/off managed with crontab not working - linux

I'm setting up a office computer to show X website from 6AM to 6PM and then turn off the monitor (Samsung TV) at 6PM and on again at 6AM by using crontab and bash scripts for the job.
Executing the scripts works fine both from the command line and from crontab but after a few hours of running the website the monitor goes to sleep.
This might be to me being a novice on linux and scripting but any help would be appreciated.
I'm running ubuntu 18.04 turned off "Dim screen when inactive", "Blank screen", "Automatic suspend" and "Screen lock" in settings>privacy/power
I've tested the scripts functionality with 5 minutes intervals in crontab and it works fine but when I run it for several hours it fails.
Script for showing the website
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
firefox -height 1080 -width 1920 google.com &
win=$(xdotool search --onlyvisible --class firefox | head -1)
while [ "$win" == "" ]; do
win=$(xdotool search --onlyvisible --class firefox | head -1)
sleep 0.1
done
sleep 10
xdotool key --window=${win} F11
sleep 5
xdotool key Page_Down
sleep 12h
killall firefox
exit 0
Script for turning the screen on
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
xset dpms force on
Script for turning the screen off
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
sleep 1; xset dpms force off
0 6 * * 1-5 /bin/bash /home/acte/startDisplay.sh
1 6 * * 1-5 /bin/bash /home/acte/hallScript.sh
2 18 * * 1-5 /bin/bash /home/acte/stopDisplay.sh
Crontab file
0 6 * * 1-5 /bin/bash /home/acte/startDisplay.sh
1 6 * * 1-5 /bin/bash /home/acte/hallScript.sh
2 18 * * 1-5 /bin/bash /home/acte/stopDisplay.sh
So from the crontab file I expect the computer to turn the monitor on at 6AM and then at 6:01AM run my script to visit the website, show that website until 6:01PM when the script exit and then turn the monitor off at 6:02 PM - rinse & repeat

Related

Docker cron scheduled job not running

I am trying to use a docker container based on an Alpine image to run a scheduled cron job, following this tutorial, but after printing the statement in my startup script, the container just exits, without running my other script.
My docker-compose service is configured as follows:
cron:
image: alpine:3.11
command: /usr/local/startup.sh && crond -f -l 8
volumes:
- ./cron_tasks_folder/1min:/etc/periodic/1min/:ro
- ./cron_tasks_folder/15min:/etc/periodic/15min/:ro
- ./cron_tasks_folder/hourly:/etc/periodic/hourly/:ro
- ./scripts/startup.sh:/usr/local/startup.sh:ro
So it runs an initial script called startup.sh and then starts the cron daemon. The startup.sh script contains the following:
#!/bin/sh
echo "Starting startup.sh.."
echo "* * * * * run-parts /etc/periodic/1min" >> /etc/crontabs/root
crontab -l
sleep 300
I dropped a sleep command in there just so I could launch an interactive shell on the container and make sure everything inside it looks good. The script creates another folder for 1min scripts. I have added a test script in there, and I can verify it's there:
/etc/periodic/1min # ls -a
. .. testScript
The script is executable:
/etc/periodic/1min # ls -l testScript
-rwxr-xr-x 1 root root 31 Jul 30 01:51 testScript
And testScript is just an echo statement to make sure it's working first:
echo "The donkey is in charge"
And looking at the root file in etc/crontabs, I see the following (I've re-run the container several times, and each time it's creating a new 1min folder, which is unnecessary, but I think not the problem here):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
The echo statement in testScript is never printed to my terminal, and the container exits with exit code 0 shortly after starting. I want to print this statement every minute... what am I missing?
In the docker compose file you have
command: /usr/local/startup.sh && crond -f -l 8
The intention is to run as a shell command, but it's not at all clear from the question that's what's going to happen; that depends on your ENTRYPOINT. Since it's defined with [] brackets, not additional shell will be provided. The command value will be passed as arguments to the ENTRYPOINT.
Assuming that will become a shell command, && in the shell runs the left hand side, and if that succeeds, then runs the right hand side. So startup.sh needs to complete before crond is executed. startup.sh ends with
sleep 300
crond is invoked only after that 300 seconds.
In either case, crond is either not invoked at all, or sleep has not been completing. The comments show that an error starting crond was discovered.
Using an entrypoint such as this is standard practice to configure the environment before, or provide runtime parameters when, invoking the main executable. To do it right, you should make sure to use exec to run the main executable so that it receives the signals that would otherwise go to the bash shell running the entrypoint script.
So at the end of startup.sh:
exec crond -f -l 8
Will replace the shell running startup.sh with crond, so that crond receives all signals (at this point the shell is gone). It's subtle but important!
In general, keep the invocation of the application as simple as possible. Case in point, your execution process was split between entrypoint, command, and startup script, with no clear interface between them. You wouldn't have gotten hung up on the invocation if you had put crond directly into the Dockerfile and left it at that. Sometimes arguments must be provided at runtime, but environment variables - which have names, not just positions - are often preferred. This keeps invocations simple and debugging straightforward. But, when that doesn't work, a shell script entrypoint is a great solution - just make sure to exec your final process!

How would I get a cron job to run per 30 minutes?

I put below into crontab,but it not working.
*/30 7-20 * * * pgrep -f crawl_index.py > /dev/null || python3.6 /htdocs/crawl/crawl_index.py >> /var/log/py-crawl.log 2>&1
*/10 7-20 * * * pgrep -f download_url.py > /dev/null || python3.6 /htdocs/crawl/download_url.py >> /var/log/py-download.log 2>&1
but when I run pgrep -f download_url.py > /dev/null || python3.6 /htdocs/crawl/download_url.py >> /var/log/py-download.log 2>&1 it works
Most of the cron executables will execute a script every 30 minutes with:
*/30 * * * * (command to be executed)
For older cron executables that don't understand the */x notation, the script can be run every 30 minutes by adding the below line in your crontab:
0,30 * * * * (command to be executed)
This command runs at the 0th and 30th minute of every hour, so basically every 30 minutes. The minutes 0 and 30 may be changed based on your requirement to 1 and 31 or 2 and 32 etc, still running every 30 minutes.
See this link for the documentation

Script run from crontab can't use xvkbd or xdotool

I'd like to refresh firefox automatically after 2 hour using simple bash script.
I've got:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
xvkbd -window Firefox -text "\Cr";
exit 0
and I'd like to run it using crontab -e. I've added task but nothing happens.
Everything is without any problem when I run this script with terminal.
I also tried xdotool in my simply script.
I'm not interested in firefox add-ons like "reload every" or "Tab auto reload", becouse every restart of firefox loses setting of add-ons.
Any answer or ideas will be highly appreciated.
Thank you.
There was something wierd with xvkbd package. Finally I found another solution.
I installed MozRepl add-on for Firefox.
It creates file mozrepl#hyperstruct.net.xpi in .mozilla directory. I looked through defaults/preferences/mozrepl.js and I found pref("extensions.mozrepl.autoStart", false); "false" I change to "true". This is the way that Firefox runs add-on automatically even if I close the browser.
I also wrote simple expect script:
#!/usr/bin/expect -f
set timeout 10
spawn nc localhost 4040
expect {
"repl>" {send "BrowserReload(), repl.quit()\r"; exp_continue}
"lost connection" {puts "ERROR: lost connection"}
"No route to host" {puts "ERROR: no route to host"}
timeout {puts "ERROR: timeout"}
}
Also I created a cron task:
00 */2 * * * /root/script.exp
crontab isn't a shell script. You should read more about the format of crontab by running man 5 crontab. If that seems too daunting for you, you should search for the myriad cron tutorials on Google. For instance, when you search for "Vixie cron tutorial", the first result is Newbie: Intro to cron, which upon brief inspection is pretty helpful to get you started.
For your particular use case, put the following in your crontab. (Either pasting it into the text editor that is opened by crontab -e, or saving it to a file and then do crontab FILENAME. I prefer the latter approach. You can view the contents of your current crontab by doing crontab -l. Read more about the crontab command by running man 1 crontab.)
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
00 */2 * * * xvkbd -window Firefox -text "\Cr"
This way the crond daemon should run the command xvkbd -window Firefox -text "\Cr" every two hours, at 0:00 am, 2:00 am, 4:00 am, etc. If you'd rather do it on 1:00 am, 3:00 am, etc., replace the last line by
00 1-23/2 * * * xvkbd -window Firefox -text "\Cr"
The first 00 is the minutes, so you can also replace that by, say, 30:
30 */2 * * * xvkbd -window Firefox -text "\Cr"
Then the command is run on 0:30 am, 2:30 am, etc.
As always, read the man page (man 5 crontab) or tutorial for more information. SO is not for complete tutorials.

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

crontab need user to login once before it runs the script

I'm trying to schedule a game server restart and vps restart at a time. I made 3 cron entries (screen terminate, vps reboot, game server start)
crontab entry for root user
30 6 * * * /sbin/reboot
crontab entry for user1
29 6 * * * sleep 48; screen -S cs1 -X quit
31 6 * * * cd /home/user1/steamcmd/hlds; ./cs1.sh
cs1.sh
screen -dmS cs1 ./hlds_run -game cstrike -pingboost 2 +map de-dust +maxplayers 31 +port 27018
The problem is the user1's second crontab entry is not working unless if I log in (via putty) after the reboot and before 6:31 AM. Can anyone experienced in this explain how to fix that?
I run a craftbukkit server that reboots every day at 0230. I found I couldn't call the startup script from crontab, but I could just run the screen command.
Here is my crontab entry for the server restart:
#reboot cd /home/mcadmin/craftbukkit && screen -dmS minecraft java -Xincgc -Xmx2048M -jar craftbukkit.jar

Resources