Questions tagged [shell-exec]

The shell_exec() PHP function provides the ability to run shell commands during the execution of a script.

The shell_exec() function provides the ability to run shell commands during the execution of a script. Other similar functions include, exec(), system(), passthru(), and the use of backticks around the command (``). It is important to note that commands executed this way will be run under the same permissions as the user that started the original process (i.e. calling this from a web page processed by Apache will run this under the same user Apache uses, typically httpd).

This functionality becomes handy when the developer wishes to execute a command which no API, library or extension for PHP exists. Example, a script you are developing needs to display the output of the UNIX vmstat command. PHP does not have visibility into such information, so the use of shell_exec('vmstat'); becomes warranted.

This function can be a security risk and, as such, this function is commonly disabled in shared hosting environments.

1065 questions
3
votes
3 answers

wget not working when called from exec() or shell_exec()

I am trying to integrate a wget command I have written into a php script. The command recursively downloads every html/php file on a website (which is required functionality that I haven't found in file_get_contents()). I have tested the wget…
Jfonts
  • 63
  • 2
  • 8
3
votes
2 answers

Failing a Jenkins job on a condition

The end goal is to search for all the files n sub-directories within a folder a on a Linux box for control m characters. If yes, do not proceed with the ANT build and fail the job. If not, proceed with the ANT build. In a Jenkins job I write this…
sumant
  • 127
  • 1
  • 3
  • 11
3
votes
2 answers

sudo: no tty present and no askpass program specified When useing shell_exec

I have a php page that is trying to run a service restart using: $list=shell_exec('sudo /sbin/service NetworkManager restart'); I needed to edit my sudoers file to let this happen. Thus: #Defaults requiretty and apache ALL=(ALL) NOPASSWD: …
mando222
  • 543
  • 3
  • 6
  • 16
3
votes
1 answer

php shell_exec and hg pull

I have written simple php script to help me update site contents when the commit is sent to bitbucket. I have following problem with it.
Jonnhy.B
  • 51
  • 3
3
votes
1 answer

php -> shell_exec() -> psexec -> my executable chain fails

I have a php from where I have to call a cmd, and from that cmd I have to start an exe using psexec. My php, cmd, psexec.exe and the exe I actually need to run are in the same folder. php: shell_exec("runas.cmd"); runas.cmd: @echo off psexec -u…
fishmong3r
  • 1,414
  • 4
  • 24
  • 51
3
votes
2 answers

Running a background process with shell_exec from the PHP built-in webserver

The PHP built-in webserver does not seem to handle shell_exec()ing background processes properly; the request hangs until the background process is complete, even if it's explicitly placed in the background with &. Example: $ ls runit.php $ cat…
sil
  • 1,769
  • 1
  • 18
  • 34
3
votes
2 answers

Why does PHP backticks and SSH not return identical values?

When I run ps cax with my ssh command line, I get the following: user@dqeb ~ $ ps cax PID TTY STAT TIME COMMAND 3277 ? Ss 12:51 httpd 6797 ? S 1:45 httpd 7190 ? Ss 0:00 gpopd.pl 7291 ? S 0:02…
Coert Grobbelaar
  • 627
  • 4
  • 14
3
votes
1 answer

How can I mock the results of git diff in phpunit

I'm writing a database migration script in PHP and I need to mock the results of a git diff in phpunit. The idea is that the git diff will only return the names of files that have been added or updated in includes/ since the previous commit. But of…
Jack Vial
  • 2,354
  • 1
  • 28
  • 30
3
votes
1 answer

shell_exec() and exec() Doesn't Show The Output

shell_exec(): I'm doing a PHP site that uses a shell_exec() function like this: $file = "upload/" . $_FILES["file"]["name"]; $output = shell_exec("leaf $file"); echo "
$output
"; Where leaf is a program that is located in the same…
Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
3
votes
2 answers

Loops with PHP shell_exec() : Use php for() or bash for-do-done?

Let's say I want to execute the command mycommand with PHP shell_exec() 10 times. Should I do a bash loop: shell_exec('for i in {1 .. 10} do mycommand -i done'); or rather a PHP loop: for($i = 1; $i <=10; ++$i) { shell_exec('mycommand -'.$i);…
Foo Bar
  • 1,764
  • 4
  • 24
  • 43
3
votes
2 answers

Running "Last" Linux Command

I'm trying to execute a linux command in PHP, here is my sample code: $command = "last -F"; $o = shell_exec($command); print_r($o); Most of the Linux commands gives me an output, but for the Last -F command, I have no output. Why is it so?
cocoboy
  • 274
  • 1
  • 3
  • 6
3
votes
0 answers

shell_exec doesn't execute from PHP but works fine in shell

user/bin/inkscape -f sample.svg -e sample.png Above command works when executes in shell, but with php shell_exec() its not executing. Any help will appreciated.
Naitik
  • 1,455
  • 15
  • 26
3
votes
2 answers

PHP: Strange Problems with shell_exec("mkdir") and shell_exec("rm -rf)

I'm working on a PHP script to automatically decompress and scan tar.gz archives located on an external USB drive, and I've run into a couple of strange problems. Once the script locates an archive, it attempts to create a temporary directory on the…
NinjaBlob
  • 111
  • 6
3
votes
2 answers

Output of shell_exec gets truncated to 100 characters

When run in shell the following command, curl -F file=@filename http://192.168.0.1 produces the following output: Accuracy = 0% (0/1) (classification) Accuracy = 0% (0/1) (classification) Accuracy = 0% (0/1) (classification) Accuracy = 0% (0/1)…
jessems
  • 1,582
  • 2
  • 11
  • 16
3
votes
4 answers

Executing shell script using php

I have a shell script deploy.sh that has the following content:- echo "0 Importing the code" eval "git pull -u origin master" echo "1 Backing up existing data in database.." // -- other code follows here When I execute the script directly using…
Sparky
  • 4,769
  • 8
  • 37
  • 52