How to stop the execution of .sh script at certain time? - sh

I have the following script which ensure that a .php file will be invoked every 3 seconds.
#!/bin/bash
for (( i=0; i<43200; i++ ))
do
/usr/bin/php /var/www/vhosts/mydomain.com/httpdocs/somefile.php
sleep 3
done
I would like to be able to stop the script excecution if the time is 23:58:59
Anyone can help me?
Thanks, Zoran

case `date +%H%M` in 2359) break ;; esac
Edit: I discovered the zero padding in the date formatting string was not portable, so I took it out. It's not useful or necessary in this case anyway.

AGAIN me.
since your script has no imput youmcan try to launch yor script in the following way:
./yourscript.sh & sleep 10 && kill %1 && fg
give it a try.
Se

I think there is no direct way without using a scripting language like python or perl.
here if yo want there is te perl script already implemented:
http://www.cyberciti.biz/faq/shell-scripting-run-command-under-alarmclock/
Utilization summary:
Create a file installTimeout.sh containing the following:
wget http://pilcrow.madison.wi.us/sw/doalarm-0.1.7.tgz
tar -zxvf doalarm-0.1.7.tg
cd doalarm-0.1.7
make
and run the command:
source installTimeout.sh
now it's transparent to you.
When you want to give a timeout to your script yo just have to run:
doalarm 20 your script
Best,
Ste

Related

How to make matlab block the shell in windows?

I use win7 and matlab2012a. I want to write a shell script to test my matlab scripts with different parameters. I use cygwin for this task. For example, alpha is the parameter and the matlab script is getall.m. The matlab script will read parameters from txt file 'param.txt'.
#!/bin/sh
# List=`seq 0.1 0.01 1`
List=`seq 0.1 0.1 0.2`
for alpha in $List
do
echo -ne "20\n61\n80\n1\n0.3\n${alpha}" > param.txt
matlab -nodesktop -r "getall;quit;" #time consuming
done
My problem is that script "getall.m" is time consuming, so I'd like to exec it one at a time. But I found that matlab command returns immediately. So the upper script will start a lot of matlab instances at the same time. I also tried the matlab command in cmd, but nothing changes. In ubuntu, matlab blocks the shell by default.
My question is how to make the matlab command to block the shell in windows?
There's a matlab -wait command line switch on Windows that will make it block.
http://www.mathworks.com/help/matlab/ref/matlabwindows.html
I don't know the "right" way to do this - but I do have a hack for you:
Make the matlab script create a file called "matlabDone" in the /tmp directory just before quitting; your shell script can go around a loop looking for this file. Once it exists, you know matlab is finished. Delete the file, and go around the loop again. Something like this:
List=`seq 0.1 0.1 0.2`
for alpha in $List
do
echo -ne "20\n61\n80\n1\n0.3\n${alpha}" > param.txt
matlab -nodesktop -r "getall;quit;" #time consuming
while [ ! -e /tmp/matlabDone ]
do
sleep 1
done
rm /tmp/matlabDone
done
Then make the last line of your matlab script create the file /tmp/matlabDone...
As I said - it's a hack...
PS I am not 100% sure what functions are available in cygwin. If you can't use sleep, I saw an interesting post suggesting that ping -n 2 127.0.0.1 > /dev/null (or equivalent ... depending on the version of ping you might need -c 2 -i 1 to get "one second per ping, count two") can be an alternative to sleep().

Change shell within Perl script

I am looking for a nice way to get the following done:
So I have a script that I need to run in Python in Unix by calling from a Perl script that was, in turn, called from my Excel VBA macro in Windows using Plink. The Python script, due to dependency issues, has to run in either csh or bash, and I will need to use export/setenv to add a few libraries before running the script. However by default, perl runs in sh shell and as such, there is no way I can add in all the dependencies and have the Python script to run.
So, I am just wondering if there is EITHER: 1. a way for me to add dependencies to sh shell in the perl script, OR 2. force my perl script to run in csh (preferred, since for some reason .bashrc for the account runs into permission issues).
Thanks a lot!
How about "3. Set the appropriate environment variable in the Perl or Python scripts"?
$ENV{'PATH'} = ...
...
os.environ['PATH'] = os.pathsep.join(newpaths + os.environ['PATH'].split(os.pathsep))
(dunno how to get the path separator in Perl, sorz)
To force the shell to csh, try the following in Perl :
`/bin/csh -c "command_name"`;
Edit:
You can use ENV variable, like this. Try that :
$s = `/bin/bash -c 'VAR_FOO=753; echo \$VAR_FOO'`;
print $s;
I ended up just change the .cshrc script, apparently the addition to PATH, for some reason, did not work for me. After that, everything runs smoothly by putting all into one line
so basically it looks something like this
/path/to/.cshrc && /python/path/to/python
Hope that helps!

Remove script itself

I would like my script to remove itself automatically since its work is done. I have added the line below to the end of my script:
unlink($0);
For some reasons, it's not working. Could you please advise what I should do? Probably, there is another approach or I could add an error message to find out why it's not removed?
Thank you.
I don't know why you can't. It works in my machine. Maybe your file system locks the file when it's running.
And you please try this:
exec "rm -f '$0'";
which replace the current Perl process with the rm one. This should release your script and has it removed.
at the end of your script:
system("sh -c 'sleep 1; rm -f $0' &");
Bit hacky, but why do you need to delete your script? Surely that means something has created it, why not have the creator delete it too?
You can't do that. Your script is still running so the file will be in use, that's why it can't delete itself.
You need to call an external script to delete the first script after it has died.

PHP CLi - Set running on cmd line then continue? Don't want bash script to wait

Hopefully a quick question. I have a .sh script running a php script - the php takes some time to complete and I want the .sh script to proceed.
Is that possible? And if so, how so?
Have you tried php somescript.php & ? The & at the end causes the sh script to continue executing.
If you want the php script to outlive the shell script, try this:
nohup php somescript.php >/dev/null 2>&1 &

Run a command as cron would but from the command line

I have a script that I'm trying to run from cron. When I run it from bash, it work just fine. However when I let cron do it's thing, I get a:
myscript.sh: line 122: syntax error: unexpected end of file
What I want is a way to run a command as if it was a cron job, but do it in my shell.
As a side note: does anyone know what would be differnt under cron? (the script already has a #!/bin/sh line)
To answer my own question: I added this to my crontab:
* * * * * bcs for ((i=$(date +\%M); i==$(date +\%M) ;)) ; do find ~/.crontemp/ -name '*.run' -exec "{}" ";" ; sleep 1; done`
and created this script:
#!/bin/sh
tmp=$(mktemp ~/.crontemp/cron.XXXXX)
mknod $tmp.pipe p
mv $tmp $tmp.pre
echo $* '>' $tmp.pipe '1>&2' >> $tmp.pre
echo rm $tmp.run >> $tmp.pre
chmod 700 $tmp.pre
mv $tmp.pre $tmp.run
cat $tmp.pipe
rm $tmp.pipe
With that, I can run an arbitrary command with a delay of not more than one second.
(And yes, I know there are all kinds of security issue involved in that)
the problem was a fi vs. if problem. Doh!
When a script works interactively and fails in cron it's almost always a PATH problem. The default PATH in a cron job process is much much shorter than in an interactive session. The typical result is a "not found" error for some system utility you're trying to run that is not on the PATH in cron.
I would guess that some command you're trying to run is not on the path, therefore the file it was supposed to create is empty and the command that's trying to read that file is giving you this error message.
You may have a "%" in your crontab.
You must escape it (with "\") or it is changed in a newline character.
There are a number of things it could be - output will be redirected elsewhere; environment variables will almost certainly be different, etc. On the information you've given, it could be related to a difference between bash and /bin/sh (which on some systems - including Debian/Ubuntu flavors of Linux - are different, and support slightly different syntax). Cron will usually run the command you give to it using /bin/sh.
Try running:
/bin/sh -c '<command>'
where <command> comes from your crontab. (Of course, if that command uses '' quotes you will need to modify it accordingly...)