Bash Script to Run PHP Script

Bash script to run php script

If you have PHP installed as a command line tool (try issuing php to the terminal and see if it works), your shebang (#!) line needs to look like this:

#!/usr/bin/php

Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).

You can also use php myscript.php.

Making Command in bash script to run php script

You need to look at the shebang line.

Placing #!/usr/bin/env php at the top of your file should work as long as you have execute permissions:

#!/usr/bin/env php
<?php
echo "Hello World";

You can then utilize the native function getopt() to get options, or parse argv yourself.

Running php script (php function) in linux bash

From the command line, enter this:

php -f filename.php

Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.

Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).

Reference: https://secure.php.net/manual/en/features.commandline.usage.php

Run bash script from PHP script

I found what the issue was. It was because of Linux SELinux feature. This feature applies a least-privilege policy and denies any unnecessary command from running on Linux. The bash script is running successfully after disabling this feature. To do so, edit the file /etc/selinux/config and change SELINUX=enforcing to SELINUX=disabled and reboot the system. THIS IS NOT RECOMMENDED FOR SECURITY REASONS, however. You may check the link below to only create some exceptions rather than completely disabling SELinux.

https://wiki.centos.org/HowTos/SELinux

Run PHP script with command line globally on win10

I finally solved it:

-Create a .bat file where your php file in, it contains:

@ECHO OFF
php %~dp0/update.php %*

-And save it as "update.bat"

-Add the directory name into "Path" in "Environment Variables"

Now you can write "update var1=abc var2 var3=def" freely in command prompt!

Bash Script can run php script manually but cannot work in Cron

You have run into one of cron's most common mistakes, trying to use it like an arbitrary shell script. Cron is not a shell script and you can't do everything you can do in one, like dereferencing variables or setting arbitrary new variables.

I suggest you replace your values into the cron line and avoid usage of variables

/usr/bin/php5.6 /home/michael/bash/checkalive.php >/dev/null 2>&1 &

Also, consider removing the trailing & as it is not necessary.

Run a PHP file every 5 seconds using Shell Script

The best thing would be to configure crontab to keep the execution or another program installed as a service.

The problem with contrab is that the minimum execution is every 1 minute. Therefore, you should create a script that executes every 5 seconds no more than 12 times. (12 x 5 seconds = 60 seconds)

Kill the process and re-run it with crontab.

Example

sript.sh

#!/bin/bash
# Do not run 12 times because this will same time as next crontab execution
for i in 1 2 3 4 5 6 7 8 9 11
do
php /home/user/www/run.php
sleep 5
done

On crontab

* * * * * /path/to/script.sh


Related Topics



Leave a reply



Submit