How to Run a Cron Job with Arguments and Pass Results to a Log

How can I run a cron job with arguments and pass results to a log?

It looks like you are searching for the log file in the wrong folder.
Try this

* * * * * cd /path/to/script.php ; ./script.php arg1 arg2 >> logfile.log

Then look for your log file in the /path/to/script folder.
It can also be a write permission problem.
Also, check your script for errors.
Your crontab command seems OK.

How to pass parameters while invoking script in cron?

The first crontab entry looks correct. Since there is no #! /bin/bash line, you might need to put bash in front of the invocation, e.g.

*/2 * * * * /bin/bash /root/todo-api/workspace/docker.sh c4e842c79337

According to docker inspect

Usage

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

the second invocation without $CONTAINER must fail, because inspect expects a name or some id.

To debug this further, follow this question How to debug a bash script? and keep the output from stderr, e.g. remove 2>/dev/null.

how to schedule python script with parameters using crontab?

Here's a simple example to demonstrate how you can schedule python scripts with args using a shell script and cronjob.

hello_world.py

import sys

def main():
print(sys.argv[1])
print(sys.argv[2])

if __name__ == '__main__':
main()

hello_world_scheduler.sh -- using such shell scripts have lots of added advantages that may come in handy in the future.

#! /bin/bash

cd /path_to_my_script
/usr/bin/python3 hello_world.py hello world! > execution_logger.log

Run

chmod +x hello_world_scheduler.sh ## to make the script executable
./hello_world_scheduler ## to run the shell script
cat execution_logger.log

The output should be

hello
world!

Just add the scheduler to cronjob -

* * * * * /path_to_script/hello_world_scheduler.sh

This should work

How to get arguments from PLESK Cron jobs

$argv[0] always contains the name of the script file, as it passed to the PHP binary.
As per screenshot, $argv[1] is '33' and $argv[2] is 'On'. You can easily check with:

echo $argv[1];

Or you can list all arguments as an array by:

var_dump($argv);

Basically, the following task is added to crontab, when scheduled via Plesk:

/usr/bin/php5 -f '/test.php' -- '33' 'On'

If test.php contains mentioned commands, the result of its' execution will be the following:

# cat /test.php
<?php
echo "The first argument is $argv[1]\n";
echo "Here the full list of arguments:\n";
var_dump($argv);
?>
# /usr/bin/php5 -f '/test.php' -- '33' 'On'
The first argument is 33
Here the full list of arguments:
array(3) {
[0]=>
string(6) "/test.php"
[1]=>
string(2) "33"
[2]=>
string(2) "On"
}

Passing parameter in crontab through shell to php script

You may not have register_argc_argv enabled in your php.ini.

Use php's getopt to get the command line arguments.

See http://php.net/manual/en/function.getopt.php

Arguments in cron jobs

HTTP and CLI are completely different environments.

If you want to adapt your script so it runs without changes in either environment, you can indeed get data from country_id=10 but you need to parse it yourself since it isn't a standard format that PHP expects. You can use parse_str(), but you need to ensure you're actually following the same encoding rules that you'd follow in an actual URL:

if (isset($argv[1])) {
parse_str($argv[1], $_GET);
}
var_dump($argv, $_GET);

If you want to write a command-line script that behaves more traditionally you can use getopt() and call it like e.g.:

/usr/bin/php /home/website/cron/my_script.php --country_id=10
/usr/bin/php /home/website/cron/my_script.php --country_id 10
$params = getopt('', ['country_id:']);
var_dump($params);

Passing $_GET parameters to cron job

The $_GET[] & $_POST[] associative arrays are only initialized when your script is invoked via a web server. When invoked via the command line, parameters are passed in the $argv array, just like C.

Contains an array of all the arguments passed to the script when
running from the command line.

Your command would be:

* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234 

You would then use parse_str() to set and access the paramaters:

<?php

var_dump($argv);

/*
array(4) {
[0]=>
string(27) "/path_to_script/cronjob.php"
[1]=>
string(13) "username=test"
[2]=>
string(13) "password=test"
[3]=>
string(9) "code=1234"
}
*/

parse_str($argv[3], $params);

echo $params['code']; // 1234

bash - how to pass arg values to shell script within in a cronjob

Alright try this way.

1) Create a separate file /mypath/dir/login.info with content like this (username/password in separate lines):

foobar
F008AR


2) Modify your test.sh like this:

#!/bin/bash
if [ $# != 4 ]; then
exit 1
fi

DIRDT=`date '+%Y%m%d'`
TSTDIR=$2/test/$DIRDATE
[ ! -d "$TSTDIR" ] && ( mkdir "$TSTDIR" || { echo 'mkdir command failed'; exit 1; } )

IFS="
"
arr=( $(<$2/$4) )
#echo "username=${arr[0]} password=${arr[1]}"

perl /home/dev/tstextr.pl -n $1 -b $2 -d $TSTDIR/ -s $3 -u ${arr[0]} -p ${arr[1]} -f $DIRDT


3) Have your cron command like this:

30 10 * * 5 sh /home/test.sh hostnm101.abc /mypath/dir test login.info >> /logs/mytst.log 2>&1

Summary

  • IFS stands for Internal Field Separator (IFS) in bash

I am using it like this:

IFS="
"

Which means make new line character as field separator (since we are storing username and password in 2 separate lines). And then this line to read file /mypath/dir/login.info into an array:

arr=( $(<$2/$4) )
  • First line (username) is read into $arr[0]
  • Second line (password) is read into $arr[1]

You can echo it to check read content:

echo "username=${arr[0]}"
echo "password=${arr[1]}"


Related Topics



Leave a reply



Submit