How to get recursive directory path using inotify-tools in terminal - php

I'm using inotify-tools where i want a notification of file which has been created in recursive directories
Till here i'm succesfull
Now i want to get directory path where a file is been created/dumped in the recursive folders
for e.g abc.txt file is dumped in data/test folder
i want the path to be data/test/abc.txt
Below is the code im using in .sh file
inotifywait -m -r --format '%f' -e modify -e move -e create -e delete /var/www/cloud/data | while read LINE;
do
php /var/www/cloud/scannner/watcher.php;
done
Kindly please help me to get the path of a dumped file in recursive directories
Cheers

Use the %w modifier:
inotifywait -m -r --format '%w%f' .......
To pass the output of inotifywait as an argument to a php script, which will read it for the argv variable you could do this:
inotifywait -m -r --format '%w%f' ....... | while read -r line
do
php script.php "$line"
done
Otherwise, if you want the php script to read the output of inotifywait from the standard input, then you can just pipe to your script:
inotifywait -m -r --format '%w%f' ....... | php script.php

Related

Include PHP command in bash script execute it and exit

I have the folowing script.
#!/usr/bin/env bash
target="anotherfolder";
dest="somefolder";
find $dest -maxdepth 1 -type f | sort -r | while IFS= read -r file; do
while /bin/true; do
files=$(ls -a "$dest" | grep -Fxv "$ignore")
if [ "$files" ];
then
php "$files" | nc 10.x.x.x 9100
mv "$files" "$dest"
break
fi
done
done
When i run the script is working only with the first file, after that is stop.
I assume i have to add an exit code after
php "$files" | nc 10.x.x.x 9100
Can you help ?
Barmar is correct. It is also not so clear why you are using find command here. You just need only one loop, use below script it might fix your problem. Also always provide the details, exactly what you are trying to do with your script i.e. your requirement that help us to provide a correct answer.
#!/usr/bin/env bash
target="anotherfolder";
dest="somefolder";
#find $dest -maxdepth 1 -type f | sort -r | while IFS= read -r file; do
#while /bin/true; do
files=$(ls -a "$dest" | grep -Fxv "$ignore")
for file in files
do
if [ "$file" ];
then
php "$file" | nc 10.x.x.x 9100
mv "$file" "$target"
#in above you need to move it to another directory
break
fi
done
#done

Can't run Linux "awk" command in script from PHP

I have the shell script "test.sh":
#!/system/bin/sh
PID=$(ps | grep logcat | grep root |grep -v grep | awk '{print $2}')
echo "Using awk: $PID"
PID=$(ps | grep logcat | grep root |grep -v grep | cut -d " " -f 7 )
echo "Using cut: $PID"
When I run the script from PHP:
exec("su -c sh /path/to/my/script/test.sh");
I got this output:
Using awk:
Using cut: 6512
So "cut" command is work but "awk" command doesn't when I run the script from PHP, but when I run it from terminal:
# sh test.sh
I can get both awk and cut work fine! This how look like the output of "ps":
USER PID PPID VSIZE RSS WCHAN PC NAME
root 6512 5115 3044 1108 poll_sched b6e4bb0c S logcat
Do I missed something?
You should learn how to debug first
You said
So "cut" command is work but "awk" command doesn't when I run the
script from PHP, but when I run it from terminal:
I wonder how ?
actually throws error like below, in CLI
$ php -r 'exec("su -c sh /path/to/my/script/test.sh");'
su: user /path/to/my/script/test.sh does not exist
You first need below syntax while debugging code
// basic : stdin (0) stdout (1) stderr (2)
exec('your_command 2>&1', $output, $return_status);
// to see the response from your command
// su: user /path/to/my/script/test.sh does not exist
print_r($output);
Remember :
su gives you root permissions but it does not change the PATH variable and current working directory.
The operating system assumes that, in the absence of a username, the
user wants to change to a root session, and thus the user is prompted
for the root password
[akshay#localhost Desktop]$ su
Password:
[root#localhost Desktop]# pwd
/home/akshay/Desktop
[root#localhost Desktop]# exit
exit
[akshay#localhost Desktop]$ su -
Password:
[root#localhost ~]# pwd
/root
Solution:
You should allow executing your script without password prompt ( don't use su use sudo )
To allow apache user to execute your script and some commands you may make entry like below in /etc/sudoers
# which awk => give you awk path
# same use in your script also, or else set path variable
www-data ALL=NOPASSWD: /path/to/my/script/test.sh, /bin/cut, /usr/bin/awk
So it becomes :
// assuming your script is executable
exec("sudo /path/to/my/script/test.sh 2>&1", $output);
print_r($output);

Twig extract tranlations not working

Am trying to extract PO files with using twig i18 extension. Following this tutorial http://twig.sensiolabs.org/doc/extensions/i18n.html i try to translate my site.
Problem is when i try to extract translations when i call in terminal:
root#debian:/var/www/html# xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP ./cache/*.php
xgettext: error while opening "./cache/*.php" for reading: No such file or directory
Check screenshot:
Checkig $ xgettext --help i dont see -R recursive search
It's trying to call "./cache/*.php" but your files are in subfolders, within the cache folder, so it should be "./cache/**/*.php".
This command should work, I guess:
root#debian:/var/www/html# xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP ./cache/**/*.php

PHP Exec freeze on run command with "head" modifier

I have a directory with a lot of subdirectories and files in it.
I'm running this command using php's exec function:
exec('find /path/to/dir -type f | head -n 300');
On ssh this commands gives me result faster than eye blink.
When I'm running it using php's exec function script freeze and processes looks like:
sh -c find /path/to/dir [...] | head -n 300
|_ find /path/to/dir [...]
So it's look like script is looking for all files in this directory and after he will just cut and return first 300 of these.
Where is problem? Why it's working well in terminal?

How to launch unoconv from php file

I want to launch the command "unoconv" from a script php.
$command = '/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf >/dev/null 2>/dev/null';
$rc = system( $command );
echo $rc;
The command return no result and the file is not created.
I think is a problem from access with www-data and unoconv.
When I'm launching the command in shell, the file is created.
Any idea?
You can add command unoconv to sudoers.
I do this in this way:
I create wrapper bash script in for example /usr/local/bin where I have command unoconv.
#!/bin/bash
if [ -z "$1" ]; then
echo "Must pass file";
exit 10;
fi
/usr/bin/unoconv -f pdf $1.rtf
after this I adding entry in /etc/sudoers.d:
www-data ALL=NOPASSWD: /usr/local/bin/unoconv.sh
And now you can call script in php:
exec('sudo /usr/local/bin/unoconv.sh '.$fileName);
Try to run
$output = `/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf`;
instead and see error messages.
For me works like this:
$cmd = "/usr/bin/unoconv -f docx files/thefile";
shell_exec($cmd);
of course you have to do this previously (if you lounch your php script from the web):
chown -R www-data:www-data files/
I have found a solution to this problem when running Apache. You have to create the home folder for the www-data user
sudo mkdir /home/www-data
sudo chown www-data /home/www-data
Lastly we will have to edit the home directory and default shell for the www-data user
sudo vim /etc/passwd
For the entry of www-data the last two strings have to be replaced respectively with
/home/www-data
/bin/bash
Simple as this
$output = shell_exec('/opt/libreoffice5.0/program/python unoconv -f rtf test.html');
Edit the path to suite your configuration.
It just works!
You may be running into an issue with LibreOffice, OpenOffice or soffice not being able to write to the current user's $HOME directory.
By running the command below I was able to identify the correct $HOME directory and see the error that was being generated.
$cmd = 'echo $HOME & unoconv -vvvv --format %s --output %s %s 2>/tmp/unoconv.debug.txt';
exec($cmd);
The verbose output of $cmd will be generated written to the file: /tmp/unoconv.debug.txt.
In my case the output was:
Verbosity set to level 5
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /usr/lib64/libreoffice/program/soffice.bin.
Failed to connect to /usr/lib64/libreoffice/program/soffice.bin (pid=32012) in 6 seconds.
Connector : couldn't connect to socket (Success)
Error: Unable to connect or start own listener. Aborting.
The command ran seemed to fine as root, and as sudo -u nobody. On seeing this output I realized there was an issue with the home directory.
Kudos to Dag Wieers for his help - I'm hoping this helps other unoconv devs with their debugging.

Categories