in , ,

PHP Execute And Kill Process In Linux

php programming

PHP is a rich scripting language when used at Shell and CLI. It’s a powerful language for accomplished Linux system tasks and cron jobs.

Configure PHP CLI

To configure PHP CLI, you will find the php.ini config file under the /etc/php directory with PHP version ie /etc/php/7.4/cli/php.iniYou can adjust the timeouts, memory limits, errors display, etc.

PHP Execute And Kill Process CLI Script Example

Here is a simple example of using a PHP script to run an external executable binary application CLI and kill it when needed during your code flow.

In this example, we will run the headless Automation browser Phantomjs to make a task, then Kill the Phantomjs process.

Phantomjs will only ping a website, and his actions and instruction will be in the hello.js file. We will also pass the parameters of ignoring and skipping the SSL certificate verification check to the Phantomjs command.

The command line must use the full path to the executable binary file. and the entire command with needed parameters will be stored into a PHP string Variable $cmd as

$cmd= '/full/path/to/the/binary/phantomjs --web-security=false --ignore-ssl-errors=true --ssl-protocol=any /path/to/javascirpt/test/file/hello.js,;

 

And The Full Script will be:

<?php
$some_run_condition=true;
$cmd= '/full/path/to/the/binary/phantomjs --web-security=false --ignore-ssl-errors=true --ssl-protocol=any /path/to/javascirpt/test/file/hello.js';
if($some_run_condition)
{
   exec($cmd . " > /dev/null &");
   sleep(300);
   exec('pgrep phantomjs | xargs kill > /dev/null 2>&1 &');
}

 

First, we used exec a PHP function to run the external binary file command (Phantomjs) in our example.

Then we make sleep for (300) sec.

Second, we killed the Phantomjs process by name using the system command. pgrep.

We pass the output to the null.

Now run the PHP script using:

# php -f /path/to/script.php

Cronjob PHP CLI Script

To cronjob your PHP script, you can use crontab. For example, run the script.php when rebooting the system.

# crontab -e

~~~~

@reboot /usr/bin/php -f /path/to/script.php

~~~~

A list of PHP executing functions are here

escapeshellarg — Escape a string to be used as a shell argument
escapeshellcmd — Escape shell metacharacters
exec — Execute an external program
passthru — Execute an external program and display raw output
proc_close — Close a process opened by proc_open and return the exit code of that process
proc_get_status — Get information about a process opened by proc_open
proc_nice — Change the priority of the current process
proc_open — Execute a command and open file pointers for input/output
proc_terminate — Kills a process opened by proc_open
shell_exec — Execute command via shell and return the complete output as a string
system — Execute an external program and display the output

What do you think?

11 Points
Upvote Downvote

Leave a Reply

Your email address will not be published. Required fields are marked *

machine learning with Python

Solid Your Practical Skills In Machine Learning With Python

PhantomJS

Install Phantomjs Headless Browser And Scrape A Website Data Using Proxy And Random UserAgent