crontab need user to login once before it runs the script - linux

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

Related

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

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

CronTab every 5 minutes reboot doesn't work

I've put the command
*/5 * * * * root /sbin/shutdown -r now
Inside "sudo crontab -e" on a Raspberry Pi, which should reboot the raspberry pi every 5 minutes. But instead nothing happens.
I've looked in "/var/log/syslog" but all it says is
Sep 24 08:55:01 raspberrypi CRON[638]: (root) CMD (root /sbin/shutdown -r now)
Sep 24 08:55:01 raspberrypi CRON[634]: (CRON) info (No MTA installed, discarding output)
Hoping someone can help me!
Thanks, Jake.
It's not a valid command, remove the "root".
Since you edited the root crontab (sudo crontab -e), the command is already executed as a root.

Crontab does not run on CentOS 7

I'm using CentOS 7, and installed "cronie"
yum install cronie
I have a shell to backup my home folder, shell's content (of course, backup.sh is 775)
#!/bin/bash
#START
TIME=`date +%Y-%m-%d_%Hh%M`
FILENAME=backupHome_123.30.150.29_$TIME.tar.gz
SRCDIR=/home
DESDIR=/backup
tar -cpvzf $DESDIR/$FILENAME $SRCDIR
#END
And add to crontab -e
00 2 * * * /bin/bash /backup/backup.sh
But crontab does nothing. When I check log at /var/log/cron only, something like that and no more error or processing log
May 1 00:26:08 app crontab[12041]: (root) END EDIT (root)
May 1 00:33:21 app crontab[12086]: (root) BEGIN EDIT (root)
May 1 00:34:25 app crontab[12086]: (root) END EDIT (root)
Anyone can give me some advises to make crontab work?
Thank you.
I found that the most useful information was actually given by
systemctl status crond
Which revealed that it failed to load correctly due to an "Unauthorized SELinux context" error.
This can happen if cron daemon isn't running.
Check it with:
pgrep cron
If command returns nothing, run:
systemctl restart crond
This should help.
You want to make sure cron is started & that it is started if your server reboots so you need two commands on CentOS 7 to make sure of this:
systemctl enable crond && systemctl restart crond
If you are used to using sudo, you can add sudo in front of above command. like this:
sudo systemctl enable crond && systemctl restart crond
YOU WILL get confirmation from system, something like:
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
Then lastly check cron is running
pgrep cron
I know that I'm late to answer you, but maybe someone will have this kind of problem. It is possible that CRON can't run the script because the path is not correct.
Your path is
00 2 * * * /bin/bash /backup/backup.sh
I guess that path should be:
00 2 * * * sh /bin/bash/backup/backup.sh
Only difference is space after bash directory and sh command at the start of CRON job.
You have to add username before your command like this for example :
1 * * * * root or username /usr/bin/php /var/www/html/yourwebsite/yourscript.php

If adding a command that repeats every 10 minutes to crontab, when does the first job run?

As part of the setup of a docker container, the following gets injected into crontab:
*/10 * * * * /opt/run.sh >> /opt/run_log.log
According to the behavior of crontab, when should the first run kick off? Should the 10 minute cycle begin instantly, or 10 minutes after this is put into crontab. Neither behavior is happening so I am trying to debug this in more depth by trying to understand the intended behavior.
This cron sandbox simulator gives you an idea:
Mins Hrs Day Mth DoW
*/10 * * * *
This run time (UTC) Sat 2016-Jan-23 0653
Forward Schedule Sat 2016-Jan-23 0700
Sat 2016-Jan-23 0710
Sat 2016-Jan-23 0720
It uses the syntax:
Every nth '0-23/n', '*/2' would be every other.
'*/1' is generally acceptable elsewhere, but is flagged here as possibly an unintended entry.
See for example "Run a cron job with Docker" (by Julien Boulay)
Let’s create a new file called “crontab” to describe our job.
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
The following DockerFile describes all the steps to build your image
FROM ubuntu:latest
MAINTAINER docker#ekito.fr
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
Then you can build the image with
sudo docker build --rm -t ekito/cron-example .
And run it:
sudo docker run -t -i ekito/cron-example
Be patient, wait for 2 minutes and your commandline should display:
Hello world
Hello world
If you replaced the first '' by '/10', you would have to wait to the next 0 or 10 or 20 or... of the hour.

Using Cron to Reboot

I'm using a Raspberry Pi for a status display, but for whatever reason it gets incredabbly sluggish after a day or so of running so I wanted to reboot it every day so I setup a cron job to do that every morning at 8:50. But, it doesn't seem to be working. Is there anything special about using cron to do a reboot?
This is my crontab for the root user:
# m h dom mon dow command
50 8 * * * shutdown now -r >> /var/log/cron.log
0,30 * * * * date >> /var/log/cron.log
The second line works just fine, but I can't seem to get the restart command to work. It doesn't even output anything to the log.
Try using the fully specified path to shutdown. date may be in the PATH in roots cron environment, /sbin may not be looked up.
You need to edit the root user crontab
sudo crontab -e
then..
50 8 * * * reboot
Save and exit.

Resources