# mirror some folders with lftp, then shutdown

## Poedel

Hi friends ..

Sometimes I need to mirror some folders from ftp. I really like lftp. But I do not quite get it's params working from the shell.

I simply want the computer to shutdown after alle jobs are done.

I tried something like this:

```
queue stop

queue mirror folder1

queue mirror folder2

...

queue wait all

```

Now there is a stopped queue to be launched from my bash.

trying this:

```
lftp -c queue start && sd
```

where sd is a sudone alias that is working fine standalone from bash.

why doesn't this programm wait for all downloads to be done like I told at the end of the queue??

After that it could exit and let sd shutdown my laptop.

Could someone please help me?

Thanx in advance!

----------

## pianosaurus

I have never used lftp, so I may be way off, but according to the lftp man page:

1: ...queue wait adds already running jobs to the queue. It doesn't look like it actually queues a wait command. It may, though. You should test this separately.

2: ...the -c option executes the commands and exits (presumably right away), while the -e option doesn't exit at all. Try queueing an exit-command after the wait all and use -e instead.

----------

## Poedel

nice idea, I tried it immediately.

I added exit to the end of the queue.

Leaving the programm bei exit then right after queueing sets the programm into nohup mode and begins the queue itself.

Okay, I tried suspend instead of exit to leave it first.

No queue start, no nohup. cool.

But!

lftp bookmarkedsite -e queue start && echo hello

did not work.

lftp startet at prompt, said there was a queue but did not download anything, just stand-by. queue start by hand invoked nothing but an empty queue now.

No download. Exiting by hand leaded to the execution of the echo command. mhh??   :Sad: 

----------

## pianosaurus

Hm. Ok, here's a completely different idea. Stick all the commands you want to run in a text file (with wait all and exit at the end), but without the queue in front of each line. Use & at the end of lines to make them run concurrently. Then run:

```
lftp -f commands.txt && echo foo
```

It looks from the man page like a site name is not accepted together with -f, so put an open bookmarkedsite on top of the text file.

----------

## Poedel

this might be usefull if the folders were always the same. But first browsing, copying into textfile etc. is quite very inconvinient, you know  :Wink: 

I came across an explaining site where the author used -c and -e in combination, so to say as a cascade. Though it is really short I still get not did the point.

He did something different from what I want to do, but I suppose to become conscious of how these two parameters work together. This should be my goal.

I've been so nasty to write an email to the author. I'm excited if he will answer *G*. I will let you know, of course!

http://tutorials.papamike.ca/pub/lftp.html <- the site I mentioned. I will keep an eye on it.. probably I'll get the point soon ..

----------

## pianosaurus

Actually, I think one of the queue-ideas you have already tried should work, if you quote the command line correctly. In your original command line, you run the command "queue", not "queue start". Try this instead:

```
lftp -c "queue start" && echo foo
```

Though I'm guessing you still need to use -e instead, and with a queued exit at the end.

By the way, the only place I can see that guy use -e and -c together is where -e is an argument to open, not lftp itself.

----------

## Poedel

I tried both of your suggestions without success  ..

----------

## pianosaurus

 *Poedel wrote:*   

> I tried both of your suggestions without success  ..

 

Oh well. Hopefully, you'll get an answer on your mail.

----------

## Poedel

okay, we could have guessed this, and it still isn't the efficient way, but it should work indeed.

This is what I had in my email:

 *Quote:*   

> Hello
> 
> I believe you are having problems because you are running two separate
> 
> lftp processes.  As far as I know lftp processes cannot communicate
> ...

 

this assumes that you know before using lftp if you wanna shutdown after queueing or not.

It was easier if there was a shutdown feature integrated.

----------

## pianosaurus

You could always request that feature if the app is still in development. In the mean time, stick this at the end of your ~/.bashrc:

```
function lftp-sleep () {

  # Get PIDs of running lftp processes

  PIDS="$(pidof lftp)"

  if [ -z "$PIDS" ]; then

    echo "Error: lftp not running!" >&2

    return 1

  fi

  # Wait for PIDs to finish

  for PID in $PIDS; do

    while [ -f /proc/$PID/cmdline ] && [ "$(head -c5 /proc/$PID/cmdline)" == "lftp"$'\0' ]; do

      sleep 10s;

    done

  done

  # Shutdown

  echo "lftp-sleep: Shutting down." >&2

  logger "lftp-sleep: Shutting down."

  shutdown -h now

}
```

When you want to shut down after a queue, do wait all && exit in lftp and run lftp-sleep in another terminal. It is also abortable with ^C, in case you change your mind. Replace the shutdown-command with an echo to test it out, as usual (and comment out the logger).

----------

## Poedel

okay, really great script, my dear   :Laughing: 

My reply took me so long as I wanted to wait for the developer´s answer fist.

Here an excerpt (last in time on top):

 *Quote:*   

> Well in that case you would still have to stop lftp and request a shutdown at the end of the queue. Your solution to watch the PID and shutdown seems like a much better solution in my eyes 
> 
> On Fri, Jan 22, 2010 at 11:53 PM, ]] poedel [[ wrote:
> 
> Hi Roger,
> ...

 

Probably I will go on using your script  :Wink: 

I did not have the opportunity to test it, but as far as I know about scripting it looks pretty good!

Thanks again!

----------

## pianosaurus

No problem! =)

I tested it using sleep instead of lftp, so I think it should work. If not, it probably just needs a tweak here and there.

----------

## Poedel

yes it does work, one little problem.

When lftp cannot download something it breaks the queue sending an error message.

The task isn't finished, the pid does not disappear, the computer does not shutdown.

Any suggestions to change the behaviour by choosing the right set ??

----------

## pianosaurus

Hm. Unless lftp has some built in mechanism for this, I don't see an easy way around that. You could see if the cmd:fail-exit setting changes this behaviour (though that may make lftp exit after the first error).

----------

