Jump to content

No speed difference found when running exec(scp) in the background


tirengarfio

Recommended Posts

Hi,

I'm trying to copy asyncronously 10 70 MB video files.

 

exec("scp -o StrictHostKeyChecking=accept-new -i /var/keys/devDevices_rsa MarTianez1.mp4 awong@10.1.1.16:/tmp/test1 2>&1 > out.log", $output, $exitCode);
...
exec("scp -o StrictHostKeyChecking=accept-new -i /var/keys/devDevices_rsa MarTianez10.mp4 awong@10.1.1.16:/tmp/test10 2>&1 > out.log", $output, $exitCode);

 

But I cannot see any difference if I remove `2>&1 > out.log`'s from the scp commads.

 

PHP 8.1, Apache2, Ubuntu 22

Edited by tirengarfio
Link to comment
Share on other sites

6 hours ago, kicken said:

Do you want to just kick off some copy commands and move on, or do you want your script to wait for them to finish and verify the copy was successful by checking the exit code?

 

I want to execute the exec("scp..") commands without waiting for the scp's to finish, what is called usually asynchronously.

Link to comment
Share on other sites

The answer you found on stack overflow is incomplete.  In addition to redirecting the output, you need to actually put the operation into the background by including an & at the end.

exec("scp -o StrictHostKeyChecking=accept-new -i /var/keys/devDevices_rsa MarTianez1.mp4 awong@10.1.1.16:/tmp/test1 2>&1 > out.log &", $output, $exitCode);

This will just kick off the process and move on.  You won't have any way in your code to determine if the process completes successfully or not.  You'd only be able to detect if there is an error in the shell that prevented the command being run (syntax error, exec failure, etc).

If you want to be able to verify the copy is complete, you need to use something more complex than exec, which is the proc_open and related functions.  These let you run a command asynchronously while monitoring it's progress.  You can handle this easier by making use of the symfony/process package to execute your commands.  If you don't want to use a library, you can check my article on fibers for an example of how to run multiple processes asynchronously using proc_open.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.