Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Complete beginner question...

Let's say I have SSH access to my website. And, I have a file:

mywebsite.com/foo/my_script.php

How can I use SSH to run my_script.php?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted
ssh username@mywebsite.com "/usr/bin/php /path/to/my_script.php"

Just make sure that the user you use to login is the user you want to run the script as.

Also make sure that you actually have /usr/bin/php installed. Running a PHP script from the command line isn't the same as starting it from say Apache.

share|improve this answer
    
I wasn't able to get it right in one line (like your example). I made it work by entering separate commands. First, login to ssh. Second "ls" and "cd" a few times to get to the file. Finally, run the file with "php my_script.php" –  Ed Talmadge Mar 30 '10 at 5:08
    
Perhaps you don't have PHP cli installed as /usr/bin/php. What does "which php", on the server, tell you? –  andol Mar 30 '10 at 6:13
add comment

You can also make the PHP file like this:

#!/usr/bin/php
<?php
    echo 'Hello world';
?>

and then just type

/path/to/my_script.php 

in SSH and it will work also

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.