Jump to content
Henry_Who

Help with exec

Recommended Posts

Posted (edited)

I figure I’m doing something basic wrong.
I have this php code.

$cmd = “at $wtime $wdate <<< “sftp -a -r -P xxx xxx@xxx.xxx.net:”.$file.” /mnt/TRFR” ;
exec ($cmd);
echo $cmd;

the “at” command is never generated atq returns blank. But if I copy the output of the echo from the web page into bash the “at” job gets created.

 

What am I missing here?

Edited by Henry_Who

Share this post


Link to post
Share on other sites
Posted (edited)
$cmd = "at $wtime $wdate <<< \"sftp -a -r -P xxx xxx@xxxx.net:".$file." /mnt/TRFR > /dev/null 2>&1\"" ;
exec ($cmd);
echo $cmd;

OK fixed the code input here.

I figure I’m doing something basic wrong.

the “at” command is never generated atq returns blank. But if I copy the output of the echo from the web page into bash the “at” job gets created.

Edited by Henry_Who

Share this post


Link to post
Share on other sites

Copy and paste the command that was outputted into your terminal and see what it does.

Share this post


Link to post
Share on other sites

Sorry that's the last thing I wrote.

"But if I copy the output of the echo from the web page into bash the “at” job gets created"

Share this post


Link to post
Share on other sites

I may have found the issue.

What I now need to know is how to tell php to use bash and not dash

exec ("/bin/bash $cmd");   does not work neither does

exec ("/bin/bash -c $cmd");

Share this post


Link to post
Share on other sites

Oh shoot, you're right. Sorry.

1. If there are any errors running the command, the shell is sending them to /dev/null and you won't see them. Don't do that. Keep the 2>&1 redirect but remove the 1>/dev/null so you can get output.
2. exec() only gives you the last line of output. Use another shell function that will give you all output.

Share this post


Link to post
Share on other sites
1 minute ago, Henry_Who said:

What I now need to know is how to tell php to use bash and not dash

PHP is going to use the default shell, but first let's see if the shell really is the source of the problem.

Share this post


Link to post
Share on other sites

From what I've read php uses the default shell

/bin/sh   this is just a link to /bin/dash    dash has no understanding of <<<

I got desperate and linked /bin/sh to /bin/bash somehow this did not get php to invoke bash as I got the same message about "unexpected redirection"

this error is only just found by me doing a tail on /var/log/apache/error.log

Share this post


Link to post
Share on other sites
Posted (edited)

In full test mode now:

This is the test code I'm using now:

$cmd = "at $wtime $wdate <<< \"echo 33333333333333333 > 0-fxxkme.txt\"";
exec ("/bin/bash -c $cmd");
echo $cmd;

The at job is never created.

/var/log/apache/error.log displays

sh: 1: Syntax error: redirection unexpected

I've change /bin/sh to link back to dash

Note in the apache log the sh:

I need to get php to use bash and not sh "aka dash" on my system

Edited by Henry_Who

Share this post


Link to post
Share on other sites
Posted (edited)

OK

with /bin/sh set to /bin/bash I now get this apache error (I restarted apache after changing the link this time)

Garbled time

This is a reference to the "at" command maybe now I need to change where the quotes are ?

 

 

if I remove the -c apache reports

/usr/bin/at: /usr/bin/at: cannot execute binary file

Edited by Henry_Who

Share this post


Link to post
Share on other sites

SOLVED !! It's not pretty but it works !

changed /bin/sh to link to /bin/bash

$cmd = "\"at $wtime $wdate <<< \"echo 33333333333333333 > 0-fxxkme.txt\"\"";
exec ("/bin/bash -c $cmd");
echo $cmd;

added escaped quotes as above, at job now created !

I've never even heard of dash. Every time I log in I get "bash" by default.

Share this post


Link to post
Share on other sites

OK so now my original code now looks like this

$cmd = "\"at $wtime $wdate <<< 'sftp -a -r -P xxx xxx@xxx.net:'$file\" /mnt/TRFR\"\"" ;
exec ("/bin/bash -c $cmd");
echo $cmd;

Figuring out where to put single quotes, double quotes and what quotes to escape was the tricky bit after getting php to use bash in stead of dash.

For those that are interested this code forms the basis of solution to fix the increasing unreliability of my Satellite Internet link. I live near whoopwhoop just beyond this side of Black Stump. (That's Aussie for in the middle of bloody nowhere).

Over the past few months my Internet regularly drops out screwing up my scheduled downloads. This code goes a long way to allowing me to resume the downloads after the link has been brought back up without human intervention.  This would not be needed if Filezilla, or any other ftp client, could schedule downloads. The Internet link generally only goes down for a few minutes. It's enough to break the sftp.

Share this post


Link to post
Share on other sites
Posted (edited)
$cmd = "\"at $wtime $wdate <<< 'sftp -a -r -P 1966 root@ns-09.fezit.net:$file /mnt/TRFR'\"" ;

Previous post was incorrect that code dropped the destination path for the sftp but still scheduled the job. This code works correctly.

Edited by Henry_Who
more info

Share this post


Link to post
Share on other sites

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.