Cron job, how to? Automate "socat"

Hi Everybody!

I want to automate some of my tasks. I need to keep “socat” connection to another host. Command are the following:

socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.0.100:7001

If everything is ok, socat will start to forward all data from remote host to /dev/ttyS0, until it will closed. But some times socat can’t start correctly and need to be restarted. How I can made it?

Sorry for my English )
Thanks in advance.

ToLive adjusted his/her AFDB on Saturday 15 Aug 2009 07:16 to write:

>
> Hi Everybody!
>
> I want to automate some of my tasks. I need to keep “socat” connection
> to another host. Command are the following:
>
> Code:
> --------------------
> socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.0.100:7001
> --------------------
>
> If everything is ok, socat will start to forward all data from remote
> host to /dev/ttyS0, until it will closed. But some times socat can’t
> start correctly and need to be restarted. How I can made it?

Do you want this to start on machine start-up or will just cron do?

If start-up sounds better then have a look in /etc/init.d/skeleton here is a
preformed template for a script that can be used as a base for such things.

Otherwise you can have a cron script as root or user.

I would attempt to do it myself but my scripting is not elegant/pretty I
would suggest you ask in the scripting group here, there are some wizz kids
in there which will probably give you a quick one liner that would do what
would take me reams of code :slight_smile:

HTH


Mark
Caveat emptor
Nullus in verba
Nil illegitimi carborundum

You can add that command into a cron table: reference here:
Cron Tables (Crontab) in Suse / openSUSE [Event Scheduler for 10.x, 11.x]

But the difficult thing would be if it fails to start. For that you need an extended script. You set the cron job to trigger at a prearranged time and run the script. The script would run the command then sleep x seconds then test if the transfer is running then if no then run the command again but if yes then end. Or like that.

Have a look at bash scripting or more advanced, like perl.

If it can be regarded as a system service you can also add it to /etc/inittab. Add a line like this:

SO:35:respawn:socat PTY,link=/dev/ttyS0,raw,echo=0 tcp4:192.168.0.100:7001

then kick init to reread inittab by doing as root

init q

It will be run when entering either runlevel 3 or runlevel 5 and if it terminates, another instance will be started.

However this might not work if networking is not up at the time the runlevel is entered and then it would try to respawn too many times and then init will disable it for a while.

Big thanks! I will try it all on monday.