PHP execute command as subcommand - execute

I have the following 2 files and am executing them on linux (debian).
File1.php
<?php
exec("php -f file2.php > /dev/null 2>&1 &");
sleep(100);
File2.php
<?php
exec("sleep 30 > /dev/null 2>&1 &");
exec("sleep 30 > /dev/null 2>&1 &");
sleep(100);
The way it currently is it first starts file1.php and fires up file2.php and immediatly begins the sleep commands. It does not wait for the first sleep to finish to continue.
The problem is that the file2.php and sleep commands are not subcommands of file1.php and I can't simply kill it to kill all subcommands.
My htop looks like this: http://dl.getdropbox.com/u/5910/Jing/2011-01-13_1611.png
I am searching for a way to have the commands be subcommands of file1.php so that I can easily kill them all :)
what I have:
'- php -f file2.php
'-sleep 30
'-sleep 30
'- php -f file1.php
basically I want this:
'- php -f file1.php
'- php -f file2.php
'-sleep 30
'-sleep 30

I haven't looked into it too much, but my first guess would be to fork it from file1.
http://php.net/manual/en/function.pcntl-fork.php
I can take a better look later, but you should be able to use pcntl_waitpid($pid) for the fork to complete.

popen and proc_open seem to do it :)
file1.php
<?php
$descs = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open("php -f file2.php", $descs, $pipess);
sleep(100);
file2.php
<?php
$desc = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a")
);
echo "asd";
$process[] = proc_open("sleep 12", $desc, $pipes);
echo "sleep!";
$process[] = proc_open("sleep 10", $desc, $pipes);
echo "asd";

Related

/Users/username/.jenv/jenv.version: Permission denied

macOS Catalina, installed jenv 0.5.4 using homebrew, using zsh, followed all the steps listed in https://www.jenv.be/
In terminal I have the following error
Last login: Tue Dec 22 10:10:15 on ttys002
/usr/local/Cellar/jenv/0.5.4/libexec/libexec/jenv-refresh-plugins: line 14: /Users/username/.jenv/jenv.version: Permission denied
The below is the code from jenv-refresh-plugins
#!/usr/bin/env bash
# Summary: Refresh plugins links
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
set -e
[ -n "$JENV_DEBUG" ] && set -x
FORCE_REFRESH=0
if [ ! -f "${JENV_ROOT}/jenv.version" ]; then
echo "NONE" > ${JENV_ROOT}/jenv.version
fi
if [ "$1" = "--complete" ]; then
echo "--force"
exit
fi
if [ "$1" = "--force" ]; then
FORCE_REFRESH=1
fi
lastVersion=$(cat "${JENV_ROOT}/jenv.version" || echo "none")
currentVersion=$(jenv --version)
if [ ! "$lastVersion" == "$currentVersion" ] || [ $FORCE_REFRESH == "1" ]; then
echo "jenv has been updated, process to refresh plugin links"
for path in "${JENV_ROOT}/plugins/"*; do
if [ -L "$path" ]; then
pluginName=$(basename $path)
echo "Refresh plugin $pluginName"
ln -sfn "${JENV_INSTALL_DIR}/available-plugins/$pluginName" "${JENV_ROOT}/plugins/$pluginName"
fi
done
fi
echo "$currentVersion" > "${JENV_ROOT}/jenv.version"
jenv doctor
[OK] No JAVA_HOME set
[OK] Java binaries in path are jenv shims
[OK] Jenv is correctly loaded
Any help, much appreciated.
I just experienced this same issue on mac- it was caused because for some reason the folder /Users/username/.jenv had become locked.
I couldn't find a way to unlock it, so i just copied it to another directory, then ran sudo rm -rf /Users/username/.jenv , copied it back, and that has solved the problem.

Executing while loop issues KSH

I'm trying to execute a ksh where in the file it contains
a = 0
max = 25
while [[$a -ne $max]];
do
echo "$a"
a = $((a+5))
done
It's giving me an error
"[[0: not found [No such file or directory]"
I will check the while loop and it says 2 brackets are needs and ne is for numerical which I did.....I don't know why it is not working....
Try this code,
a=0
max=25
while [ $a -ne $max ]; #Changed here
do
echo "$a"
a=$((a+5))
done
For integers you can use
max=25
for (( a=0; a<max; a+=5 )); do
echo "a=$a"
done

How do I write usage of a command unix

I have to write a command that rules with certain arguments.I need to show a message of usage of this command.I tried this:
if [ $1 = 'help' ]; then
echo Usage: '-a arguments at-author....'
fi
It doesn't work.Why?
case "$1" in
"help" )
echo "Usage ...." ;;
*) echo "others..." ;;
esac

How can I assign command output to a variable in GNU make target rule?

At a BASH prompt, I can do the following:
~/repo$ HISTORY_LOG=$(git log $(get_old_version)..$(get_new_version)); [[ ! -z ${HISTOR_LOG} ]] && ( echo "Some header"; echo "${HISTORY_LOG}" )
Where git log is demonstrably simplified version of what I actually have.
In a make file I have the following command as part of a target:
$(OUTPUT): $(INPUT)
...
echo "Some header" > $(LOG_FILE)
git log $(shell get_old_version)..$(shell get_new_version) >> $(LOG_FILE)
How can I rewrite the make target to behave like the bash command?
If I do the following line-feeds are being stripped:
$(OUTPUT): $(INPUT)
...
HISTORY_LOG="$(shell git log $(shell get_old_version)..$(shell get_new_version))" ; \
[ -z "$${HISTORY_LOG}" ] && \
true || \
(echo "Some header" ; echo "$${HISTORY_LOG}" )
when run looks like:
~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93 Author: Jamie <Jamie#mymail.com> Date: Mon Jun 25 18:46:27 2012 -0400 Issue #468: This sucker's been sped up.
and what I prefer would be:
~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93
Author: Jamie <Jamie#mymail.com>
Date: Mon Jun 25 18:46:27 2012 -0400
Issue #468: This sucker's been sped up.
I think the issue is the that make executes commands in /bin/sh and not /bin/bash. Regardless I'm looking for a portable solution if there is one.
Make's shell is eating your newlines. Just stop using it. Instead of $( shell get_old_version ), escape the $:
$$( get_old_version )

How to use loops statements in unix shell scripting

How to use loop statements in unix shell scripting for eg while ,for do while. I'm using putty server.
for: Iterate over a list.
$for i in `cat some_file | grep pattern`;do echo $i;done
while loop looks pretty much like C's.
$ i=0;while [ $i -le 10 ];do echo $i;i=`expr $i + 1` ;done
If you are going to use command line only, you could use perl, but I guess this is cheating.
$perl -e '$i=0;while ($i < 10){print $i;$i++;}'
More data
http://www.freeos.com/guides/lsst/
#!/bin/sh
items=(item1 item2 item3)
len=${#items[*]}
i=0
while [ $i -lt $len ]; do
echo ${items[$i]}
let i++
done
exit 0
As well as the 'for' and 'while' loops mentioned by Tom, there is (in classic Bourne and Korn shells at least, but also in Bash on MacOS X and presumably elsewhere too) an 'until' loop:
until [ -f /tmp/sentry.file ]
do
sleep 3
done
This loop terminates when the tested command succeeds, in contrast to the 'while' loop which terminates when the tested command fails.
Also note that you can test a sequence of commands; the last command is the one that counts:
while x=$(ls); [ -n "$x" ]
do
echo $x
done
This continues to echo all the files in the directory until they're all deleted.
to the OP, to iterate over files
for file in *
do
echo "$file"
done
to generate counters
for c in {0..10}
do
echo $c
done
using for loop
max=10
for (( i=0; i<=$max; i++ ));
do
echo $i
done
to iterate through a file in KSH
while read line ; do
echo "line from file $line"
done < filename.txt
echo "sample while loop"
i=0;
while [ $i -le 10 ]
do
echo $i
(( i++ ))
done

Resources