0

I have a PHP web application that schedules Linux "at" jobs using the PHP function exec(). The scheduled task is another PHP script. Somehow, under certain conditions that I suspect are related to scheduling the same job simultaneously, a job that is scheduled for well into the future is immediately executed. For instance, yesterday I was testing this functionality and an "at" job executed prematurely.

I redirect the output from the job, as limited as it is, to a file in /var/log/ called at_job.log. The job executed at 21:17 (9:17pm) UTC yesterday (Feb 20th), when the job was scheduled.  Here is the log file contents, as of February 21st, 17:57 (5:57pm) UTC:

..
job 72 at Thu Feb 21 23:07:00 2019
job 73 at Thu Feb 21 23:17:00 2019

And here is the "at" job queue as of the same time (February 21st, 17:57 UTC):

# atq
73      Thu Feb 21 23:17:00 2019 a staging

Wondering how this is possible, and what I did wrong to cause it. I have put in safeguards to avoid scheduling jobs at the same time since then but I'm not 100% sure that is what happened here.

UPDATED:

Here is the command I ran below (slightly changed to hide some details):

/usr/bin/php -q /PATH/TO/PHP/SCRIPT/cli_admin.php cli product_list_edit 20 | at -m 2206 21.02.2019 >> /var/log/at_log.log 2>&1

This appears to result in the script running immediately AS WELL as something being scheduled for later (duplicate entry was me testing the command directly in a putty window):

atq
75      Thu Feb 21 22:06:00 2019 a staging
76      Thu Feb 21 22:06:00 2019 a root
74      Thu Feb 21 21:43:00 2019 a staging
5
  • 1
    (1) I don’t understand.  What are you showing us in the first code block?  Is that “the output from the job, as limited as it is”, which you write to /var/log/at_job.log (which is what you seem to be saying)?  What does it mean?  What is it supposed to prove?  (2) Imagine that you’re explaining this problem to somebody who isn’t you and can’t read your mind.  (That shouldn’t be hard, because this is what you are doing.)  Try to think of all the information that such a person would need to be told in order to understand your question, and provide that information.   … (Cont’d) Commented Feb 21, 2019 at 18:29
  • (Cont’d) … Please do not respond in comments; edit your question to make it clearer and more complete. Commented Feb 21, 2019 at 18:29
  • 1
    Please show the code used for scheduling and logging.
    – Kusalananda
    Commented Feb 21, 2019 at 18:31
  • I have edited my question. Thanks for the feedback. Commented Feb 21, 2019 at 20:36
  • Does the PHP command output the commands to execute? at reads the commands to execute from its standard input.
    – Kusalananda
    Commented Feb 21, 2019 at 20:38

1 Answer 1

2

This looks as a misunderstanding of where at gets the commands to execute from.

The at command reads the command to execute from its standard input.

If you want at to execute your php command at a particular time, you need to feed that command (not the output of it) into at:

echo '/usr/bin/php -q /PATH/TO/PHP/SCRIPT/cli_admin.php cli product_list_edit 20' | at ...

Alternatively,

at ... <<END_JOB
/usr/bin/php \
    -q /PATH/TO/PHP/SCRIPT/cli_admin.php \
    cli product_list_edit 20
END_JOB

(where ... are the option for at).

Or, to read the commands from a file,

at -f filename ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .