Henry_Who Posted April 9, 2020 Share Posted April 9, 2020 (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 April 9, 2020 by Henry_Who Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/ Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 (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 April 9, 2020 by Henry_Who Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576618 Share on other sites More sharing options...
requinix Posted April 9, 2020 Share Posted April 9, 2020 Copy and paste the command that was outputted into your terminal and see what it does. Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576619 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 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" Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576620 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 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"); Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576621 Share on other sites More sharing options...
requinix Posted April 9, 2020 Share Posted April 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576622 Share on other sites More sharing options...
requinix Posted April 9, 2020 Share Posted April 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576623 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576624 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 (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 April 9, 2020 by Henry_Who Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576625 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 (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 April 9, 2020 by Henry_Who Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576626 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576627 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576628 Share on other sites More sharing options...
Henry_Who Posted April 9, 2020 Author Share Posted April 9, 2020 (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 April 9, 2020 by Henry_Who more info Quote Link to comment https://forums.phpfreaks.com/topic/310520-help-with-exec/#findComment-1576630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.