Turnip
Turnip

Reputation: 36702

How to execute .sh files one after another with php

I need to run a series of six .sh files on the server.

An example of one of the .sh files:

wget ftp://xxxxxx:[email protected]/accommodation.xml.zip
unzip accommodation.xml.zip
php accommodation.php
rm -rf accommodation.xml.zip
rm -rf accommodation.xml

I tried running the following from a php file:

echo shell_exec('sh accomodation.sh');

Which was stupid because the file appears to execute repeatedly and I think I've just taken down the server. Whoops.

I've inherited this site and have never used .sh files before. I'm also a php novice.

How would I go about running the files only once and then running the next?

Many thanks

Upvotes: 0

Views: 680

Answers (1)

zuloo
zuloo

Reputation: 1330

you can do all this from within PHP, you do not need any shell-script.

/* get the file via ftp */
// connect to server
$ftp = ftp_connect('ftp.interhome.com');
// login
$login = ftp_login($ftp,"username","password");
// download file to tmp.zip
$file = ftp_get($ftp, 'tmp.zip', 'accommodation.xml.zip', FTP_BINARY);
// disconnect from server
ftp_close($ftp);

/* unzip the file */
// new zip-instance
$zip = new ZipArchive;
// open downloaded file
$res = $zip->open(’tmp.zip’);
// check if file is readable
if ($res === TRUE) {
  // extract to current directory
  $zip->extractTo(’./’);
  // close zip-file
  $zip->close();
}

/* your code from accommodation.php goes here */

// delete files
unlink('tmp.zip');
unlink('accommodation.xml');

voila

Upvotes: 2

Related Questions