The idea is that when wget is running and downloading something, I can just add another URL that will be downloaded once the current download is finished. I only want to download 1 file at a time. I wrote this script
#!/bin/bash
test=/tmp/wget-download-link.txt
echo -n "$test" | while IFS= read -N 1 a; do
wget -o /tmp/wget.log -P /mnt/usb -i /tmp/wget-download-link.txt
if [[ "$a" == $'\n' ]] ; then
wget -nc -o /tmp/wget.log -P /mnt/usb -i /tmp/wget-download-link.txt
fi
#printf "$a"
echo download finished
done
The script will check for any new lines that consist of URLs, if there's any, it will rerun wget again, the problem is that this script will just keep looping, wget will download the same file continuously and just rename them if it already exists. How do I make wget re-run if there's any new URLs in the wget-download-link.txt file but stop it when the file already exists?
#msturdy I run your script but wget redownload and rename files that already exist, my script:
#!/bin/bash
test=/tmp/wget-download-link.txt
l=$(wc -l $test)
tail -n $l -f $test | while read url; do
wget -o /tmp/wget.log -P /mnt/usb -i /tmp/wget-download-link.txt
done
my wget-download-link.txt file:
http://media2.giga.de/2014/11/angel-beats-kanade.jpg
http://juanestebanrojas.com/wp-content/uploads/2014/06/angel-beats-wallpapers-4.jpg
http://images5.fanpop.com/image/photos/30100000/Angel-Beats-new-life-angel-beats-30142329-2560-909.jpg
http://kristenhazelkannon.files.wordpress.com/2013/06/angelbeats2.jpg
Downloaded files:
angel-beats-wallpapers-4.jpg
angel-beats-wallpapers-4.jpg.1
Angel-Beats-new-life-angel-beats-30142329-2560-909.jpg.1
Angel-Beats-new-life-angel-beats-30142329-2560-909.jpg
angel-beats-kanade.jpg.2
angel-beats-kanade.jpg.1
angel-beats-kanade.jpg
angelbeats2.jpg
the script keeps running, and will just rename files to .1 .2 .3 etc.
SOLVED WITH THIS
while [ true ] ; do
urlfile=$( ls /root/wget/wget-download-link.txt | head -n 1 )
dir=$( cat /root/wget/wget-dir.txt )
if [ "$urlfile" = "" ] ; then
sleep 180
continue
fi
url=$( head -n 1 $urlfile )
if [ "$url" = "" ] ; then
mv $urlfile $urlfile.invalid
continue
fi
mv $urlfile $urlfile.busy
wget $url -P $dir -o /www/wget.log -c -t 100 -nc
mv $urlfile.busy $urlfile.done
done
Related
To run one command in the background, it works well.
$cmd = 'ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null &';
exec($cmd, $output, $return_var);
Then I need to sleep for some time before the ffmpeg command. I refer to How do I run multiple background commands in bash in a single line? which works well directly in the bash console.
While not works in the below PHP script, which will return when the bash command finishes running.
$cmd = '(sleep 5; ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null) &';
exec($cmd, $output, $return_var);
I think you also need to handle sleep's stdout/stderr.
( sleep 5 > /dev/null 2>&1; ...; ) &
Or you can put the redirection after ( ... ):
( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
To run multiple commands from on bash command; concatenate using &&.
For instance:
sleep 5 && echo hello && sleep 2 && echo world
This trick can be particularly useful in cron tasks to be able to sequence multiple items within the same minute, as a different sleep value can be used as an offset before starting the command.
I use ansi2html to covert colors for HTML
But when I use exec in php for run bash file, the output is not correct.
exec("inxi.sh 2>&1", $returnOut, $stdout);
echo $returnOut[0];
<pre style="color:#bbb;white-space:pre-wrap;word-wrap:break-word;overflow-wrap:break-word">␃12System: ␃␃12Host␃ TiTAN ␃12Kernel␃ 5.3.0-59-generic x86_64 ␃12bits␃ 64 ␃12compiler␃ gcc ␃12v␃ 9.2.1 ␃12Console␃ N/A ␃
If I run bash file with terminal its return:
<pre style="color:#bbb;white-space:pre-wrap;word-wrap:break-word;overflow-wrap:break-word"><span style="color:#55f">System:</span>
inxi.sh
#!/bin/bash
inxi -xxx -C -D -G -I -m -M -n -R -s -S --usb -c 2 | ansi2html -n
StartPHPDaemon
#!/bin/bash
currdate=$(date +'%m%d%y')
curpath=$(readlink -f ${0%/*})
php_file=$curpath/ExecuteJobFromQueue.php
base_php=$(basename $php_file)
for pids in $(ps aux | grep $base_php | grep -v grep | awk '{print $2 '})
do
kill $pids
echo "Killing $pids"
done
#echo $currdate >> $curpath/logs/StartService_${currdate}.LOG
#date +'%R:%S' >> $curpath/logs/StartService_${currdate}.LOG
nohup php $php_file "E" > /dev/null 2>&1 &
ExecuteJobFromQueue.php
...// rest of the code
$server_config_path=getenv('CONFIGPATH');
wh_log("Server config path: ".$server_config_path,"INFO");
...// rest of the code
I want to get the value of CONFIGPATH from /etc/profile
When I run StartPHPDaemon from the path where the file is putted then it fetches value using following command ./StartPHPDaemon.
Output(Log):
[15-Jan-2020 11:01:33] INFO: Server config path: /home/Project/Workspace/ConfigFile/dmimasterserver.config
But when I run it from root path using command sudo sh /home/Project/StartPHPDaemon then value is getting blank.
Output(Log):
[15-Jan-2020 11:01:33] INFO: Server config path:
I have tried all the stackoverflow solutions but no result.
/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
CONFIGPATH="/home/Project/Workspace/ConfigFile/dmimasterserver.config"
export CONFIGPATH
/etc/profile is read only if bash is interactive, or non-interactive, but invoked with the --login option. I would start the daemon by
bash --login ./StartPHPDaemon
and, for debugging, do a
echo CONFIGPATH=$CONFIGPATH
in that script.
Here is my code:
$pdf = '/Users/macbookpro/Desktop/q.pdf';
$swf = '/Users/macbookpro/Desktop/q.swf';
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
exec($command2,$out,$status);
var_dump($output);
The output is NULL and no SWF is generated. However, if I output the command and copy it to terminal, it works. How do I solve this?
exec runs as the user running the script. Apache user likely doesn't have the PATH variable telling it where to look for programs, so instead of
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
Try adding the location of pdf2swf, something like:
$command2 = '/bin/pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
And make sure that the apache user has permission to get to the executable, and permission to execute it.
chmod a+x /bin/pdf2swf
Of course replace /bin/ with where ever pdf2swf really lives for all the example code in this answer.
How can I auto restart a process when it is dead?
I am currently doing it like this:
#!/bin/bash
while true;do
ps -aux 2>/dev/null |grep redis_subscribe|grep -v grep >/dev/null
if [ $? -ne 0 ];then
php /data/www/wwwroot/app.eclicks.cn/oil/index.php public/redis_subscribe subscribe 2>&1 >>/data/cilogs/manitor/image_upload.log &
fi;
sleep 10;
done;
#!/bin/bash
while true;do
ps -aux 2>/dev/null |grep redis_subscribe|grep -v grep >/dev/null
if [ $? -ne 0 ];then
php /data/www/wwwroot/app.eclicks.cn/oil/index.php public/redis_subscribe subscribe 2>&1 >>/data/cilogs/manitor/image_upload.log &
fi;
sleep 10;
done;
2>/dev/null is to redirect "Warning: bad syntax, perhaps a bogus" to /dev/null
and, >/dev/null is the same.
hope to help you!