如何在命令中将参数传递给linux [英] how to pass arguments to linux at command
问题描述
我需要稍后运行mail.php文件,而不是让用户在提交register.php时等待发送验证电子邮件.
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.
因此,我选择在一分钟后使用 at 命令在命令行中运行mail.php(在register.php中调用):
So I chose to use the at command to run mail.php ( invoked in register.php ) in command line 1 minute later:
但是当我处于at命令的交互模式时,我只能将参数发送到该php文件.
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
由于我希望这是自动的,因此我需要在运行时使用shell脚本:
Since I want this be automatic, so I need to use at run a shell script:
at -f mail.sh
但是我找不到传递 {email} 参数的正确方法,
But I could't find a proper way to pass the {email} argument,
我尝试在Shell中设置了一个可变的环境,但也徒劳无功:
I tried to set an environment varaible in Shell but also in vain:
在 register.php 文件中,我写道:
shell_exec('export email=foo@bar.com');
shell_exec('at -f mail.sh now + 1 minute');
在 mail.sh 中,我写道:
#! /bin/bash
php mail.php $email
推荐答案
您可以从stdin中读取命令,而不是从文件中读取命令. (bash的)here-doc语法在这里很好用:
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 test@test.com
EOF
');
这篇关于如何在命令中将参数传递给linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!