Cron not passing params to PHP script - cron

I have a cron job set like
php /home/novinarb/public_html/index.php --uri="cron/24satahr"
but the 'uri' param doesn't get to the php script at all. I also tried without the -- in front of uri but still nothing. Any ideas?

A more robust method would be to accept command-line arguments in your PHP script with getopt() or $argv and making it executable. An example with $argv called script.php:
#!/usr/bin/php
<?php
if (isset($argv[1])):
echo $argv[1];
endif;
?>
Make it executable:
chmod +x script.php
And execute:
./script.php "cron/24satahr"
Will output:
cron/24satahr

I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!

Is the php script running at all?
I suspect you need to provide the full path to php in your crontab line. Even though cron jobs run as you, they don't have any of your login environment set up; this means they don't have your $PATH.

Related

Incrontab output to file not working

I've got an incrontab rule set up to react to a file being added to a particular directory on my ubuntu box, and run a script. This works fine, but my goal is to print the output of that script to a file.
I've tried a few different ways to go about it, and the only way I've been able to get it working so far is to take the generated command that incrontab creates, and run it myself. So my thinking is that its possible, I'm likely just missing something obvious to an experience linux user.
I've shortened some of the commands for brevity's sake. "watchdir" "scriptPath" and "arg1" are all paths. I am bringing in two arguments to the script, arg1, and the filename wildcard from the cron job.
I've tried:
(These run the script, but don't output to file)
watchdir IN_CREATE scriptPath arg1 $# >> /home/ubuntu/logs/log-$# 2>&1
watchdir IN_CREATE scriptPath arg1 $# &>> /home/ubuntu/logs/log-$#
(These do nothing at all)
watchdir IN_CREATE /bin/bash scriptPath arg1 $# >>
/home/ubuntu/log/log-$# 2>&1
watchdir IN_CREATE /bin/bash scriptPath arg1 $# &>>
/home/ubuntu/log/log-$#
If I run 'tail /var/log/syslog' and grab the command generated from the incrontab below and paste it into the shell, it works fine
watchdir IN_CREATE scriptPath arg1 $# &>> /home/ubuntu/logs/log-$#
This works
scriptPath arg1 mission-LHPUQ7ezcF0s0UwVgUR.txt &>>
/home/ubuntu/logs/log-mission-LHPUQ7ezcF0s0UwVgUR.txt
Any insight as to what I could be missing?
I had a similar problem and found out, that incron simply ignores the output redirection and hands them over to the script as arguments:
https://askubuntu.com/questions/1164166/incrond-does-not-execute-custom-script
So your solution may be to first call a wrapper script that does the output redirection to the log file.
If you watch a directory, then $# holds the directory path and $# the file that triggered the event. If you watch a file, then $# holds the complete path to the file and $# is empty. Reference.

storing the variable value while run from cron

sh
!/bin/sh
export count=$(sqlplus -s 'username/passwd#xyz'<
When I run this script manual , script runs fine.
When I keep it in crontab entry as follows
27 16 * * * /www/abc/a4.sh >a4.log
The a4.log doesnot have any data echoed.
Please let us know how to hold the value returned to a variable via sql query
Instead of putting
&>> your_file.log
behind a line in
crontab -e
Try using
#!/bin/bash
exec &>> your_file.log
…
at the beginning of a BASH script.
Also use >> which appends to the file rather than overwriting to it.

BASH shell script works properly at command prompt but doesn't work with crontab

Here is the script that I want to execute with crontab.
#!/bin/bash
# File of the path is /home/ksl7922/Memory_test/run_process.sh
# 'mlp' is the name of the process, and 'ksl7922' is my user account.
prgep mlp > /home/ksl7922/proc.txt
# This line will give the number of the process of 'mlp'
result=`sed -n '$=' /home/ksl7922/proc.txt`
echo "result = ${result}"
# if 'mlp' processes run less than six, than read text file one line and delete
# it, and execute this line.
if ((result < 6)); then
filename="/home/ksl7922/Memory_test/task_reserved.txt"
cat $filename | while read LINE
do
# Delete line first.
sed -i "$LINE/d" $filename
# Execute this line
eval $LINE
break;
done
else
echo "You're doing great."
fi
After that, I editted crontab and checked with crontab -l
*/20 * * * * sh /home/ksl7922/Memory_test/run_process.sh
This scripts works properly from command line, however, it doesn't work properly with crontab.
It seems like shell script works with crontab anyway, because 'proc.txt' file was generated, and the first line of 'task_reserved.txt' is removed.
However, I didn't see any messages, and result file of 'mlp' processes.
Since I'm not good at English, so I'm afraid that you guys don't understand my intention.
Anyway, can anyone let me know how to handle this?
My bet is the PATH environment variable is not correctly set within cron. Insert
echo $PATH > /tmp/cron-path.txt
to see what value it currently has. Perhaps you need to manually set it to a proper value within your script.
This is actually FAQ
https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
https://askubuntu.com/questions/117978/script-doesnt-run-via-crontab-but-works-fine-standalone
If you don't have any mail installations on your system for cron to forward error messages from your script, it's a good practice to manually redirect all error messages to your preferred location. Eg.
#! /bin/bash
{
date
prgep mlp > /home/ksl7922/proc.txt
... snip ...
fi
} &> /tmp/cron-msg.txt
Have you checked the execute permission for the script? The file should have executable permission.
ls -ltr /home/ksl7922/Memory_test/run_process.sh
chmod 755 /home/ksl7922/Memory_test/run_process.sh

How to create file with instruction of commands?

I have a file name myFirstFile that contains certain commands.
But I am not able to excecute them.
If I want to execute this as a program, which code should be implemented?
If you want to execute your program, it should start with:
#!/bin/sh
It's the generic script file "header". It indicates that the script is a shell script (if it's bash script you should have #!/bin/bash, etc.). If you want to execute it, you should call chmod +x ./myFirstFile to give privileges to call it as program, and then you can start your script normally: ./myFirstFile.
Make this file executable* and give it *.sh extention like:
"myFirstFile.sh"
Than run it from terminal (or crontab - it can do things for you when you sleep :) ) like:
cd directory/you/have/that/file
sh ./myFirstFile.sh
*Im not shure that making it executable is the most secure thing you can do. All my sh scripts are and I never digged into this issue, so make sure its cool
Also make sure you have "#!/bin/bash" in first line - sometimes it helps (dont know why, Google it)
edit: for example my script for starting Minecraf server looks like this
start.sh
#!/bin/bash
BINDIR=$(dirname "$(readlink -fn "$0")")
cd "$BINDIR"
while true
do
java -Xmx3584M -jar craftbukkit.jar
echo -e 'If you want to completely stop the server process now, press ctrl-$
echo "Rebooting in:"
for i in {5..1}
do
echo "$i..."
sleep 1
done
echo 'Restarting now!'
done
You have to make the file executable:
chmod +x myFirstFile
Then you can execute the commands in it:
./myFirstFile

Test run cron entry

I added a cron job recently, but made a mistake in the path while giving the command and hence, the job never succeeded. Is there some way to test the cron changes we have done?
Please note that I had indeed copied and pasted the command from my command line and it was just an stray keypress that caused this.
When I want to test my cron jobs I usually set the interval very low and monitor the logs closely. When I am convinced the entry is correct, I set the interval back to a sane value.
For example, run job every two minutes:
*/2 * * * * echo "Hello World"
And the I run tail -f on my log file (/var/log/syslogon debian).
This question has also been asked on serverfault and has garnered a couple additional answers
The following is a paraphrased version of Marco's solution:
(Not sure if best etiquette is not providing a link only answer or not copying someone else's solution)
Create a environment file with a temporary cron entry
* * * * * /usr/bin/env > /home/username/cron-env
Then create a shell script called run-as-cron which executes the command using that environment.
#!/bin/sh
. "$1"
exec /usr/bin/env -i "$SHELL" -c ". $1; $2"
Give it execute permission
chmod +x run-as-cron
and then it is then used like this:
./run-as-cron <cron-environment> <command>
e.g.
./run-as-cron /home/username/cron-env 'echo $PATH'
Joshua's answer does not work for me. Two problems:
Variables in cron-env file are not exported (set -a needed).
Script is still tied to current tty (setsid needed).
The script run-as-cron should be
#!/bin/sh
. "$1"
exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null
Not enough rep' to fix his answer or add a comment...
use command crontab -e
This will open a vim editor and all you got to do here is
* * * * * /somepath/urscript.sh , make sure you have the appropriate spaces between dates and the path of the script
After the execution , you can check in the /var/spool/mail there will a complete trail of the script execution or errors.
For testing there is no way .. but in case ur sh urscript.sh works then cron tab will have no problem as it is exactly same thing what u do manually.

Resources