# App to test tcp service



## nORKy (Mar 10, 2012)

Hi,

I search an application, or library that tests a tcp service (is alive ?) and execute an action if not. What I want to do exactly?I want to build a PHP application cloud service. I have many servers. Each runs many applications with apache and pph-fpm, If one server goes down, I want to start a new php-fpm instance and reload the web application in all servers that are up. I hope you understand  

I don't want a nagios. *J*ust a little daemon that detects very fast if a tcp service (HTTP) is down on a server and executes a script.


Thanks you.


----------



## SirDice (Mar 11, 2012)

Build something yourself using a bit of shell script and nc(1) (netcat).


----------



## Quip (Mar 11, 2012)

If you want something complex, look at sysutils/heartbeat

But I think it is better to write something simple in a shell script with the *nc*:


```
nc -z [url]www.example.com[/url] 80 || echo 'cannot connect'
```
or

```
echo -e "GET / HTTP/1.0\n\n" | nc [url]www.example.com[/url] 80
```

Or you can use the *fetch* to download a web page, parse its content by *grep* or *sed* and then run your command.


```
fetch -T 5 -o /dev/null -q [url]http://www.example.com/[/url] 2> /dev/null || echo 'cannot fetch'
```

(replace *echo* with your own command)

You can run it from *cron* each minute, or use it in an endless *while* loop


----------

