I am using a bat to run a php on win2003 scheduler. Is there a way to check the processes and see if the file is still running.
How about your batch file like this: (pseudocode, as it's been a while)
:makerandom
make som random var, microtime, whatever, we call it %x%
check if file exist, if it does, goto makerandom
call the script with %x% as argument
:check
if file exist %x% goto check
:done
in the php-script:
create the file specified by the argument
... script here ...
delete the file
In your scheduled task's .php file: Use getmypid() to get the PHP process' ID (PID) and save it to a file.
Next time your .php file is called, use $tasks = shell_exec('tasklist.exe'); to get a list of all active processes, then read the previously saved PID and look it up.
Honestly, I don't know if this is the best solution or not.
Try out Sysinternals Process Utilities.
http://technet.microsoft.com/en-us/sysinternals/bb896682
The pslist utility is just what you need (given a pid tells if it's running setting an env variable)
Regards
PS: with pslist I suggest evaluating also the pskill utility
Related
I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual
I have a php script with an unlimited while, it must run 24/7
in another php file, how can I check is that file running on server or stopped?
how can i send a signal to apache to stop and re-execute that file?
I'm going to assign numbers to files. The file that is running 24/7 will be the first file and the file that will change the state of the first one will be called the second file.
Now, the first file can write to a file or in database, let's say, every 10 minutes. This way you know if it's running by checking that file and the last date when it wrote that file. So you create a second file or database table and write in it the state you want the first file to be. Example: active or disabled. Now you read the file/table with the first file, if it's disabled, you stop executing the script.
easiest might be to save timestamped status in memcache or other shared location, and have the other php script check the status timestamp
it's easy to kill an apache process and hit the page again, that will restart the script. Or you can add a signal handler to restart on SIGHUP
You cannot check whether a specific file is running. You'll have to check whether a process is still running with that file. That also means this isn't something Apache can do for you. You'll either have to:
1) use an OS-dependant kill-signal to the process running the script
2) check for a kill-signal from inside the script
The former requires a lot of privilige on the server, so it's probably easier to do the second.
The easiest way is for the running file to write to a database or file somewhere to signify that it´s still going, and to read the same location for a signal to stop every so often. If the running process sees a stop-signal, it can simply break from whatever loop is keeping it going.
The second script can set the signal at whatever point and for whatever reason, and the other script will quickly after terminate.
If the first script terminates, write a file to a disk called error.txt, and then make the second script check for this every minute or so.
The second script once spots an error.txt file will be signaled to restart your first script. More realtime would be to use a database and with the current timestamp.
This post is about some automation tasks, and also to satisfy my curiosity.
Is this scenario possible, can anyone offer any practical pointers?
Run a shell script
shell_exec(bash script);
Bash script like:
Run a shell;
read file for input;
pass input to shell;
fetch result from shell;
write to another file for output.
Keep in infinite loop.
Write input commands to file for example:
wait few seconds
read output file for result
depending on output, write new input commands to file
the loop continues.
I'm going to leave a shameless link to a post where I demonstrate doing this, "expect in php": http://codehackit.blogspot.be/2012/04/automating-command-line-scripts-in-php.html
Basically it's just a wrapper around proc_open(), which returns FDs for writing and reading to another processes stdin/stdout. http://php.net/manual/en/function.proc-open.php
In order to avoid problems with partial reads and writes (due to io buffering and races), you may want to consider using a directory the in/out-box like so:
Create your command files in the dir with "temporary" names (e.g. "cmd_`date +%s`.txt.tmp")
When you're done writing to a given command file, close it (to flush the buffers), then rename it to remove the ".tmp". Rename is atomic within a filesystem.
Have the consuming bash "daemon" only look at "cmd_*.txt" (not .tmp) and when it's done with a given command, either delete the cmd file or rename it to give it a ".done" suffix. (If you need multiple parallel worker daemons, you can probably even rename to ".processing" to "claim" a cmd while you work on it. Just be sure to check the return code of the rename when you do so to see if another worker out-raced you.)
Do likewise for the output files.
I have a command line PHP script that runs constantly (infinite loop) on my server in a 'screen' session. The PHP script outputs various lines of data using echo.
What I would like to do is create a PHP web script to interface the command line script so that I can view the echo output without having to SSH into the server.
I had considered writing/piping all of the echo statements to a text file, and then having the web script read the text file. The problem here is that the text file will grow to several megabytes in the space of only a few minutes.
Does anyone know of a more elegant solution?
I think expect_popen will work for you, if you have it available.
Another option is to used named pipes - no disk usage, the reading end has output available as it comes.
The CLI script can write to a file like so:
file_put_contents( '/var/log/cli-log-'.date('YmdHi').'.log', $data );
Thereby a new log file being created every minute to keep the file size down. You can then clean up the directory at that point, deleting previous log files or moving them or whatever you want to do.
Then the web script can read from the current log file like so:
$log = file_get_contents( '/var/log/cli-log-'.date('YmdHi').'.log' );
As Elias Van Ootegem suggested, I would definitely recommend a cron instead of an constantly running script.
If you want to view the data from a web script you can do a few things....one is write the data to a log file or a database so you can pull it out later....I would consider limiting what you output if you there is so much data (if that is a possiblity).
I have a lot of crons email me data, not sure if that would work for you but I figured I would mention it.
The most elegant suggestion I can think of is to run the commands using exec in a web script which will directly output to the browse if you use : http://php.net/manual/en/function.flush.php
I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.
Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file.
And this file run every time which time is stored by user in database.
By using Cpanel I can do that but I don't want to use cpanel.
Please give me some proper and working solution, I really need it.
Thanks in advance.
Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.
From within the crontab you can use one of two strategies to run a php script_
- call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable.
- use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.
This might act as a starting point for you to experiment with:
#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');
// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab",
implode(' ',$time),
'/usr/bin/beep');
// execute command
$result=system($command);
// output result of execution
if ($result)
echo "Result: $result\n";
else
echo "FAILURE!\n";
?>
Works for me when called from CLI.