You are not logged in.

#1 2009-02-24 17:43:01

T0MAS
Member
Registered: 2007-02-22
Posts: 12

Please help with cron hourly

I have following script in /etc/cron.hourly
#!/bin/sh
su -l archie -c "ssh -o TCPKeepAlive=no -o ServerAliveInterval=10 -o ExitOnForwardFailure=yes -n -N -T -R 10002:localhost:22 archie@linux.box"

It's doing reverse ssh connection to linux.box, problem I have is that when it runs it doesn't go to the background any my cron.hourly job just stays in the process list:
root      1904  0.0  0.2   2776  1188 ?        S    17:01   0:00 /bin/sh /usr/sbin/run-cron /etc/cron.hourly
root      1905  0.0  0.2   2776  1152 ?        S    17:01   0:00 /bin/sh /etc/cron.hourly/reverse_ssh
root      1906  0.0  0.1   2200   864 ?        S    17:01   0:00 su -l archie -c ssh -o TCPKeepAlive=no -o
archie    1907  0.0  0.4   6072  2480 ?        R    17:01   0:00 ssh -o TCPKeepAlive=no -o ServerAliveInterval=10 -o

How do I run reverse_ssh script so that run-cron releases it?
Thanks very much for your help

Regards,
Tomas

Offline

#2 2009-02-24 18:01:37

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Please help with cron hourly

#!/bin/sh
su -l archie -c "ssh -o TCPKeepAlive=no -o ServerAliveInterval=10 -o ExitOnForwardFailure=yes -n -N -T -R 10002:localhost:22 archie@linux.box" &
exit 0

right?

Offline

#3 2009-02-24 18:20:59

T0MAS
Member
Registered: 2007-02-22
Posts: 12

Re: Please help with cron hourly

Thanks, will have a try!

Offline

#4 2009-02-24 19:36:24

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Please help with cron hourly

hurrrr!?
Why are you running this in a cron?

I recommend running it under something like daemontools instead (with no-background).
That way if the connection dies, it will auto-restart.

Alternatively (and my recommendation against), add the '-f' flag to ssh to send it to the background in your cron.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2009-02-25 10:39:37

T0MAS
Member
Registered: 2007-02-22
Posts: 12

Re: Please help with cron hourly

Hi,
Thanks it works OK with the cron (goes in the background) when I add & and exit 0 to the script.

cactus: I'll try -f option, can you please tell more about daemontools? Where I can get it and where can I read about it?

Cheers,
Tomas

Offline

#6 2009-08-25 15:23:41

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Please help with cron hourly

1. Doing this:

command &
exit 0

will always be the same as doing this:

command &

2. You could include your entire script in the crontab line. But of course you don't need to.

3. Since you're backgrounding the ssh tunnel (whether using '&' or by using the '-f' flag), cron will see the process _it_ started as having completed. So an hour later, it won't refrain from running the job again (it would refrain if the process it started were still running). So you're going to get repeated attempts to open this tunnel, even if it's already open. I guess they'll fail, since port 10002 on your server is already bound to the existing tunnel. But it may generate lots of error output? Are you having the output of your cron jobs mailed to the local owner of the job (this is the default)?

4. If you wanted to keep the tunnel open all the time, and reopen it when it fails, what you're doing is one way to achieve that. If on the other hand you just want the tunnel open for a short time, you could change your ssh line from

ssh -o TCPKeepAlive=no -o ServerAliveInterval=10 -o ExitOnForwardFailure=yes -n -N -T -R 10002:localhost:22 archie@linux.box &

to

ssh -o TCPKeepAlive=no -o ServerAliveInterval=10 -o ExitOnForwardFailure=yes -n -f -T -R 10002:localhost:22 archie@linux.box sleep 10

This replaces the no-command switch "-N" with the command "sleep 10", and uses the "-f" flag instead of "&". It will hold the tunnel open for 10 seconds. If some other process uses the tunnel within 10 seconds, tunnel will continue to stay open until that process (and all other processes) stop using it. You can make the sleep for longer if you like.

Offline

Board footer

Powered by FluxBB