LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-03-2011, 03:44 PM   #1
bcnx
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Rep: Reputation: 0
Question Push command to the background in a cron script


Hi all,

I'm writing a script that performs backups from partitions on remote servers. I used to do this with ssh, but that seems to be somewhat slow, so I switched to netcat.

I start a listener on the receiving server (which runs the script) like this:

netcat -l -p 7000 | dd of=imagefile &

and then I make the remote and start a dd piped to netcat.

This works quite well started manually and the listener is waiting in the background. However, when I have cron run the same script to automate it, the netcat listener is not started, probably because the "&" thing does not work (or there is something with the piping that behaves differently then).


Anyone any idea on how to accomplish this?

thx!!!!

B.
 
Old 02-03-2011, 04:01 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

& is special in cron. You need to escape it (\&).

But why background the process? It is not attached to any terminal when you run it from cron.

Hope this helps.
 
Old 02-04-2011, 04:19 AM   #3
bcnx
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Original Poster
Rep: Reputation: 0
It should be in background because the script will not continue otherwise until this process is stopped in foreground. And the next step is to tell the remote server to start dumping to the netcat listener. This will not happen as long as the listener is not pushed to the background.

So you are basically telling this is not possible because we run from cron?

thx!

B.
 
Old 02-04-2011, 05:16 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,
Quote:
Originally Posted by bcnx View Post
It should be in background because the script will not continue otherwise until this process is stopped in foreground. And the next step is to tell the remote server to start dumping to the netcat listener. This will not happen as long as the listener is not pushed to the background.
There is no foreground and/or background when you execute from cron, like I said before: The jobs run from cron are not connected to a terminal (simply said: they already run in the background).

Quote:
So you are basically telling this is not possible because we run from cron?
No I'm not.....

If you put the following line in your crontab: 10 * * * * netcat -l -p 7000 | dd of=imagefile the following will happen:

1) At ten past the hour crontab will launch the netcat command,
2) the netcat command will silently wait (execute a ps -ef | grep netcat to see the process) until called upon.

When you run dd if=imagefile | netcat <some.ip.number> 7000, the transfer is started and when finished all processes involved (on both machines) stop (re-run the ps -ef command shown earlier).

There's no need to push anything to the background.

The fetch command (dd if=imagefile | netcat <some.ip.number> 7000) can also be run from crontab on the other machine, just make sure it is started later then the first one.

Hope this helps.
 
Old 02-05-2011, 05:39 AM   #5
bcnx
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Hi,

makes sense. This compromises a bit the ambition to centralize things I need to create a separate cron rule for every LV that needs dd-ing), but I guess it will do.

thx!!!!


B.
 
Old 02-05-2011, 05:52 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
You're welcome
 
Old 02-05-2011, 05:58 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

About the centralize part:

You can write a script with all the netcat statements in it and run that script from cron.

Inside the script you do have to put the netcat command into the background!

I.e:
Code:
#!/bin/bash

netcat -l -p 7000 | dd of=imagefile0 &
netcat -l -p 7001 | dd of=imagefile1 &
netcat -l -p 7002 | dd of=imagefile2
Safe the script (lvm.dd.sh for example), make it executable and let cron kick it of:

10 * * * * /full/path/to/lvm.dd.sh

At ten past the hour the script is launched, which in turn launches 3 netcat commands.

Hope this helps.
 
Old 02-06-2011, 03:01 AM   #8
bcnx
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Original Poster
Rep: Reputation: 0
I see, that centralizes a bit more. Even more handy would be to have a sort of daemonized listener that could be started from the original script. I was thinking along the lines of xinetd but I'm not sure this will work. Don't really think so because of the pipe.

thx!

B.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
Bash script run via cron not executing MYSQL command mackstar Linux - Server 4 04-23-2009 05:01 AM
Script runs fine from command line, but not when attempted from Cron hwest Linux - Newbie 8 12-11-2008 06:48 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration