# read pid-file and check process



## Cycrus (Dec 22, 2010)

Hey guys ,

i will writa a shell script.

the script to check a process by a pid.

the pid is in a file with the name PID 

how i can read the pid from this file ? and then check a process (is run or is not run) ?

can you help me there ?

greetz

Cycrus


----------



## DutchDaemon (Dec 22, 2010)

Pick up any online manual on shell scripting, or simply script around pgrep(1) or something like that.


----------



## Cycrus (Dec 22, 2010)

Hey ,

i have now this here : 



```
ps -p `cat pid`
if [ $? = 1 ] 
then
echo "Not Running."
else
echo "Running."
fi
```

in the pid file is the pid 50100 this is the pid from the process.
when i kill the process and run this shellscript with "sh pidtest.sh" i become output "Running" .. but the process is not running when i check with ps ax

what i make wrong ?


----------



## DutchDaemon (Dec 22, 2010)

Should work, in principle, just like:


```
ps -p `cat pid` > /dev/null 2>&1 && echo Running || echo "Not Running"
```


----------



## SirDice (Dec 23, 2010)

It's probably better to use pgrep(1), it has better return codes to check on. It also has an option to read a pid file directly (-F switch). Also check if the pid file actually exist before using it.


----------



## Cycrus (Dec 23, 2010)

Hey ,

thanks but is not work with my codes..

[CMD="ps -p `cat /usr/server/pid` > /dev/null 2>&1 && echo "Running" || cd /usr/server/ && ./server &"][/CMD]

ehhm also all is right then ?
output is "running" and he start the server

Mfg


----------



## Cycrus (Dec 23, 2010)

Okay now i can test it..

but my crontab is not really running i have type follow commands :


```
crontab -l
crontab /etc/crontab
crontab -e
```

follow line i typed in this file



```
*/1	*	*	*	*	root	/usr/sv2/game/ && ./pidtest.sh &
```

this is for every minute or ?
but it is not start the server -,- when he is not running

then i have made the line so : 


```
*/1	*	*	*	*	root	/usr/server/game/pidtest.sh > /dev/null
```

but here the same.. the server not start when he not run.

greetz


----------



## SirDice (Dec 23, 2010)

Remove the username from the crontab entry. See crontab(1).


----------



## Cycrus (Dec 23, 2010)

Yes but now i have this so :


```
* * * * * /usr/sv2/game/pidtest.sh
```

and the script doesnÂ´t start every minute..

the script works.

when i make `sh pidtest.sh` then comes Running or he starts the server

how i can see my mails ? There is every "you have a new mail"

greetz


----------



## SirDice (Dec 23, 2010)

Keep in mind that PATH is rather limited when starting from cron. 



			
				Cycrus said:
			
		

> how i can see my mails ? There is every "you have a new mail"


mailx(1)


----------



## Cycrus (Dec 23, 2010)

He not restart the Server when is not running.. -,-

`crontab -e`
entry : 

```
* * * * * /usr/pidtest.sh > /dev/null
```

pidtest.sh :


```
cd /usr/server/
ps -p `cat /usr/server/pid` > /dev/null 2>&1 && echo "Running" ||  ./server
```

is not running the server starts not new 

what can i make ?


----------



## SirDice (Dec 23, 2010)

Remove the >/dev/null in the crontab. 

The echo will make sure you'll get an email. If you get an email every minute from cron you'll see the output in there. Also look in /var/log/cron for any errors.


----------



## DutchDaemon (Dec 23, 2010)

And format your posts, please.


----------

