Run screen and command crontab

ぃ、小莉子 提交于 2020-08-05 10:11:11

问题


I'm trying to add a new crontab that open a screen window and execute php command inside this window.

What I'm trying:

sudo crontab -e
10 0 * * * * screen -d -m php /var/www/script.php

This doesn't create a screen, but, if I remove after -m, creates normal. This code returns that I have to run on a terminal

10 0 * * * * screen -d

How can I do this? It's possible?

Thanks.

Edit

Figured out. What I had to do is send some commands to attached screen, like this:

screen -S sessionname -X stuff 'command'`echo -ne '\015'`

echo -ne '\015' emulates pressing the Enter key.

It was mentioned here: https://superuser.com/a/274071


回答1:


A similar question was asked in How do I use crontab to start a screen session?

In cron, you do not have a terminal (see for example Linux: Difference between /dev/console , /dev/tty and /dev/tty0). screen wants to run in a terminal. Getting it to run in that situation requires special handling.

According to its documentation, the combination of -d and -m options is treated specially:

-d -m

Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

In your example,

php /var/www/script.php

is a command to be run. It may rely upon environment variables to be set to make it run properly (for instance a particular PATH). The accepted answer in the other question shows how one might source the shell initialization (.profile) for example, to set the environment. Your shell might use .bash_profile, etc., but the principle is the same.



来源:https://stackoverflow.com/questions/30266676/run-screen-and-command-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!