1

There is a PHP script I would like to execute by a cron-job but it has to be executed every seconds or every 2 seconds. (PHP file updates the cover photo of a page via opengraph)

So I decided to write a shell script which is below. But how does php act when executed by a shell script, is it works normally like requested from a browser or what happens ? Does sessions works ?

#!/bin/bash

while true; do
    /path/to/file.php
    sleep 1
done;

echo "Stopped" | mail -s "Cron script has stopped." [email protected]
2
  • little about PHP CLI SCRIPTS. They are more like other standalone scripts like for example perl or python. I dont think you can use sessions in them. What is your goal? Commented Mar 13, 2013 at 9:54
  • For a detailed list of environment try a phpinfo log into a file from your file.php. Commented Mar 13, 2013 at 10:05

3 Answers 3

5

The difference is that you won't have $_GET, $_POST and other http specific stuff available. Sessions also won't work (why would you need them?). You obviously won't be able to set cookies, headers and other such things.

Other than that you can pretty much ignore the fact that you are "in a shell".

See here for more details: http://www.php.net/manual/en/features.commandline.differences.php

Sign up to request clarification or add additional context in comments.

2 Comments

There is a access token in a text file, for performance issues, php script reads it once then stores in session. Also I can't get POST and GET but I can POST right? Because this is how facebook api works.
You could use memcached, redis, apc, ... for that.
2

Take all the browser aspects away from PHP and that's how it'll act. You get no sessions (unless you were to call it with cURL and store the cookie somwehre).

You get no $_GET or $_POST - instead you need to use $args - but if you're not passing in any variables this isn't really relevant to you.

You'll obviously need to ensure that you've given your script execute permission, otherwise, call it using /path/to/php /path/to/file.php.

Take a look at these resources, they should help you out.

http://www.php.net/manual/en/features.commandline.differences.php http://www.php.net/manual/en/features.commandline.php

Comments

2

When you execute php from the command line (CLI) you enter another environement.
There are multiple differences

Some of them are

Configuration

When executing from command line, you run another php.ini file. (Usually /etc/php5/php.ini)

Permissions

On the web, you usually execute PHP with www-data user.
When executing from command line, script will be executed as the current user.

Arguments

You do not have access to $_GET, $_POST, $_FILES... superglobals anymore.
Instead, you have new superglobals so as $argv and $argc

Sessions

You do not have access to session nor $_COOKIES

Relative Paths

The path that the script uses is relative from where the command started
So, careful when using relative ./ path in your script

Eg:

$ pwd 
/path/to/project/

$ cat app/script.php
<?php
echo getcwd(), PHP_EOL;

$ php app/script.php
/path/to/project

$ cd app
$ php script.php
/path/to/project/app

You can get the current directory by using getcwd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.