简体   繁体   中英

Run php in command line on CentOS7

I expected this to be quite simple but I cannot make it work. I'm trying to run a php script in command line (to finally be able to automate it in a cron.)

The file is outside of the public web folder to avoid executing it from a webbrowser.

File is called 'daily.php' and contains :

#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>

then in my terminal I run (as root):

/usr/bin/php daily.php

but it simple outputs me the full source code without the hashbang.

So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.

I had a look in the man page and I found :

-f <file>        Parse and execute <file>.

Tried this but still the same output.

Why is this? how can I interpret this file?

Ok

I found the answer after 2 hours of trouble, and just after posting this question.

Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :

<?php
?>

and not

<?
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM