0

I have created a script that works as expected in shell but cron job does not seem to run it. when I check my cron mail, I see that it prints "hltv is not running" but does not create a screen session.

#!/bin/bash
if [[ $(netstat -a | grep '27027') == *27027* ]]; then
echo "tour 2 hltv is running"
else
echo "hltv is not running"
/usr/bin/screen -A -m -d -S tour2 ./start_hltv +connect xxx.xxx.xx.xx:27015 -port 27027 +exec hltv.cfg
fi

I have replaced my ip with xx.xxx.xx.xxx for privacy reasons.

Further details.. Well, I have a centos vps that I use for hltv (related to half life game) that runs on port 27027. Every once in a while, it crashes, so I was trying to create a script that would check if something is happening on that port and if not, it means process is crashed and need to start it.

I created script and named it "autorestart" and ran

chmod +x autorestart

This is the output i get from my cron job mail, but screen -ls does not show tour2 as valid screen.

Message-Id: <20140330021701.71D591A5B@hltvvps.localdomain> Date: Sat, 29 Mar 2014 22:17:01 -0400 (EDT)

hltv is not running

Thanks

Edit. Got this working.

#!/bin/bash
if [[ $(netstat -a | grep '27027') == *27027* ]]; then
echo "tour 2 hltv is running"
else
echo "hltv is not running"
cd /usr/hlds/tour2
chmod +x ./run_hltv (run_hltv has screen command)
sh run_hltv
sylar
  • 101
  • 1
  • 1
    it would help if you provide more information and show what research you've done so far. – Drew Khoury Mar 30 '14 at 03:15
  • changed details. I am not very familiar with bash scripting and still trying to learn. – sylar Mar 30 '14 at 03:18
  • what happens when your run the screen command manually? does it output anything on failure, if so it would be useful to output to a log file. – Drew Khoury Mar 30 '14 at 03:28
  • no errors. runs just fine and creates the process. if i exec script from shell, it runs fine as well – sylar Mar 30 '14 at 03:34
  • 5
    Are you sure ./start_hltv is the correct path when running as root? maybe you could troubleshoot by using an absolute path or checking what the current directory is when a cronjob runs. – Drew Khoury Mar 30 '14 at 03:39
  • What's your cronjob look like? – Alexej Magura Mar 30 '14 at 05:17
  • Here is the cronjob. * * * * * sh /usr/hlds/tour2/autorestart I have put it in /etc/crontab. autorestart and start_hltv are in same folder – sylar Mar 30 '14 at 09:01
  • why are `autostart` and `start_hltv` in `/etc/crontab`?! o.O – Alexej Magura Mar 30 '14 at 14:50
  • @sylar Also, are you ___sure___ that you want `autorestart` to run, every minute of every hour, every day of the month, every month, and every day of the week? Why don't you just have it run once, at like 1am, and then use something like [cronwhip](http://xyne.archlinux.ca/projects/cronwhip) to make sure that it never gets missed? – Alexej Magura Mar 30 '14 at 14:51

4 Answers4

0

Your script itself looks fine, except that you're using a relative path for the start script (./start_hltv). By default on CentOS machines, the crontab is run from the filesystem root directory /, which is done by this line:

HOME=/

In order to keep all your cronjobs working smoothly now and in the future, I would advise not to alter this line. Instead, just use the full path in your script. So replace ./start_hltv with it's full path: /usr/hlds/tour2/start_hltv.

You might also need to replace the +exec hltv.cfg with the full path, so it becomes +exec /usr/hlds/tour2/hltv.cfg.

Oldskool
  • 2,005
  • 1
  • 16
  • 26
0

... as Oldskool say you have to specify the path to cron what to do with the script try making the script executable chmod 755 scriptfile. or in cron specife the bash path. For example i have my raspberry pi alarm clock script make to execute every day at specific time. the script is in python so what iv done in cron is this

25 7 * * * python /home/scriptfolder/clock.py

every day in 7:25 am this script will be execute ... because you have specify the path in the script for what interpreter to use you just have to place in cron like this

* * * * * bash /path/to/script/file     

or

* * * * * /path/to/script/file.sh 

but here you must make the script executable

IvanAK
  • 147
  • 1
  • 1
  • 11
0

It's clear that your autorestart script is being run by cron (because you get the email saying "hltv is not running"). If screen itself was failing, I'd expect to see error messages from that in the same message. This suggests that screen is starting successfully but then dying very soon after. Have you tried adding the -L option to screen so that it logs output?

/usr/bin/screen -L -A -m -d -S tour2 ./start_hltv +connect xxx.xxx.xx.xx:27015 -port 27027 +exec hltv.cfg

Output should appear in $HOME/screenlog.0. You probably don't want that on all the time but it might help to show you what is failing.

Paul Haldane
  • 4,457
  • 1
  • 20
  • 31
0

In my opinion, relative paths and/or direct names are fine in scripts run by cron or by any other means. You just need to make sure the current directory and the environment are correct. For direct command names, you just need to set the PATH variable accordingly and for relative names you need to change direcotory. It's best to perform both of these steps at the beginning of your script.

The fact that the output of your script is included in the mail means that it's already being run by cron. (IvanAK's answer seems to be useless here. Paul's answer may help you with the screen.)

While Oldskool would solve that switching all paths to absolute paths, I would just fix the environment and current directory at the beginning.

#!/bin/bash
cd /path/where/you/run/it/successfully
...

The cron configuration and the details on current directory, current user, environment, etc, are all things you should have specified in the question. But if you did, you would probably find the answer yourself.