longtengaa
longtengaa

Reputation: 300

how to pass arguments to linux at command

I need to run a mail.php file later instead of keeping the user to wait for the validation email to be sent when they submit for register.php.

So I chose to use the at command to run mail.php ( invoked in register.php ) in command line 1 minute later:

But I can only send parameters to that php file when I was on the interactive mode of the at command.

at now + 1 minute
at> php mail.php {email}     # {email} is the argument I want to pass

Since I want this be automatic, so I need to use at run a shell script:

at -f mail.sh

But I could't find a proper way to pass the {email} argument,

I tried to set an environment varaible in Shell but also in vain:

In the register.php file, I wrote:

shell_exec('export [email protected]');
shell_exec('at -f mail.sh now + 1 minute');

In the mail.sh, I wrote:

#! /bin/bash
php mail.php $email

Upvotes: 5

Views: 4262

Answers (5)

Boycott A.I.
Boycott A.I.

Reputation: 18871

I know the original question has been satisfactorily answered, but if you are using popen in your PHP script to perform the at command, you can include arguments (as GET parameters) like this:

$target_script_path = "path/to/your/target/script";
$time = "now";
$file = popen("/usr/bin/at $time", "w");
//$cmd = "/usr/bin/php $target_script_path"; // Working example with no arguments supplied.
//$cmd = "/usr/bin/php $target_script_path?test_param=test_value"; // Trying to use a GET parameter like this does not work!
$cmd = "/usr/bin/php $target_script_path test_param=test_value"; // Working example *with* argument!
fwrite($file, $cmd);
pclose($file);

Then you can pick up the test_param value in your target script with:

$test_param = $_GET['test_param']; // "test_value"

Upvotes: 0

sNICkerssss
sNICkerssss

Reputation: 6430

1) For Linux. Check file

/etc/at.deny

Remove user "www-data" if exist.

2) Schedule simple command with php.

Linux

exec('echo "mkdir /tmp/testAT" | at now + 1 minute');

Windows

exec('at "16:45:01 20.09.2017" cmd /c mkdir C:\Server\_debug\1\testAT');

3) Schedule Symfony framework command.

exec('echo "php /var/www/mysite.com/app/console system:test_command param1 param2 --env=test" | at 11:14 AM 19.09.17');

Upvotes: 0

Havenard
Havenard

Reputation: 27854

You can use this:

shell_exec('echo php mail.php [email protected] | at now + 1 minute');

Upvotes: 8

hek2mgl
hek2mgl

Reputation: 157967

You can read commands from stdin rather than from a file. (bash's) here-doc syntax works nice here:

shell_exec('at now + 1 minute <<EOF
    php mail.php [email protected]
EOF
');

Upvotes: 5

Axel Amthor
Axel Amthor

Reputation: 11096

in one go: shell_exec('export [email protected]; at -f mail.sh now + 1 minute');

or just: shell_exec('php mail.php [email protected]');

Upvotes: 0

Related Questions