I have an issue about how to run screen through crontab, I made a script which check if a previous session is running and if not restart a screen.
cf screenshots below
crontab
restard_td.sh
When I run the script manually it's works fine, by cron, i can see my output "down, i will restart it " in my log file but no screen available and my script doesn't run.
Has someone got an idea ?
you can try checking the cron log, and use the screen full path.
I fixed it by using screen full path and python full path in my script.
like that
/usr/bin/screen -dmS team /usr/local/bin/python /home/admin/scripts/rattrapage/team.py
Related
I am developing an application on Linux CentOS server. I need to automatically after registration of each use server create a screen to run some codes in loop for user.
When I use bash script to create new screen, it can't open screen and run commands in that screen.
For example, I want to open an screen and I run a php file in that screen. I have created a bash script test.sh but when I run this using cron tab it doesn't work.
screen
php php.php
Please tell what can I do to solve this?
It's quite easy to do this and such a thread already exists.
screen -d -m yourcommand
I have a shell script contain loop like this:
while true
do
if ... ; then
...
else
...
break
fi
done
I want this script to run backend while OS start. I have try to add this script into /etc/rc.d/local.rc as startup script. But OS start too long, and after OS start up, this script did not exist.
So how to add this script into backend while OS start up? And I need this script also could be start up by hand in backend. Thank you~
If you are scheduling on either Linux or Mac, then you can schedule through crontab.
Open terminal.
$crontab -e #use sudo to run as administrator
#add below line
#reboot sh /absolute_path/script.sh
Give the absolute path of the script, save the crontab and exit.
The script will start running from the next reboot.
The following command : display=:0.0 in bash script is used. Then when calling export, I can see it's there.
However, after a while, this command is no more in the export list and I have to do it again.
Note
There is only 1 session running and I'm doing nothing on the command line. There may be a program running in background.
This is a ssh session (putty).
What could be causing this?
I was logged with ssh. I realised I sometime got logged off and on automatically. Then the export command would be off because it's a "new" session. Cannot explain why this happen with putty though.
The cron job used to working well and suddenly stopped working
1 * * * * /usr/bin/python3 /home/roy/update.py
It can still run manually on the command line.
Then I tried to debug it by the following command:
/usr/bin/python3 /home/roy/update.py 2>&1 >> /home/roy/cron_error_report.txt
There is no error shown in the cron_error_report.txt either.
Can anybody help me?
Make sure cron is running
sudo service cron status
I hope my answer can help others. It really take a long time to figure it out.
I moved a file used for my current python program to a shared folder. I exported the shared folder to PYTHONPATH.
So there is no problem while I run the script in the command line. However, cron could not run it. So I have to move the file back to my current folder and the cron starts to work again.
The main problem I'm having is to background a screen session from Hudson-CI. The shell steps are that I need to start a screen session from a script that is launched by another script. Heres' a simple test:
test.sh:
#!/bin/bash
myscreen.sh
myscreen.sh:
#!/bin/bash
screen -dm -S myscreen pingit.sh
pingit.sh:
#!/bin/bash
ping google.com
If I run ./myscreen.sh I get a screen launched that runs the ping continuously without a problem.
If I run ./test.sh, the screen is never started. I'm assuming there's something basic that I'm either forgetting or not understanding, but I can't figure out what. I thought this would work.
The real reason I want to do this is to have Hudson CI launch a continuous-test script which starts as a screen session so that it can continue in the background. What I'm finding is that the screen session terminates once the task is completed in Hudson.
Any ideas on why I can't launch a persistent screen session from a grand-parent script? Or any ideas on how to deal with this?
This is on OSX 10.6, with screen built from source (so it should work the same as linux I think).
If I run your test.sh, I get the error message
./test.sh: Zeile 2: myscreen.sh: Kommando nicht gefunden.
i.e. command not found. You'll have to write ./myscreen.sh, if the current directory is not on the path. (Is it for you? It should not.) The same is valid for the screen call.
Changing both files to
#!/bin/bash
./myscreen.sh
and
#!/bin/bash
screen -dm -S myscreen ./pingit.sh
I can start my screen without any problems.
I'm on Linux (OpenSUSE) with
$ screen --version
Screen version 4.00.03 (FAU) 23-Oct-06
here.
I don't know why I did not find the following references before, but these were the links that helped me solve the problem:
https://serverfault.com/questions/155851/run-gnu-screen-from-script
http://wiki.hudson-ci.org/display/HUDSON/Spawning+processes+from+build
There are 2 issues here - one of screen being persisted after being launched by a grand-parent process. The other that hudson terminates a session after it completes its task.
The screen problem is resolved by zombie'ing the process as follows:
screen -d -m -S myscreen && screen -S myscreen -X zombie qr && screen -S myscreen -X screen pingit.sh
The Hudson-CI problem turns out to be a bug that's easily resolved per the above link. The solution is to add BUILD_ID=something into the shell script. So if the test.sh script from above is actually the Hudson Build shell execute, then it would have to be changed to:
#!/bin/bash
BUILD_ID=dontkillthisprocess
myscreen.sh
Once both of these steps are implemented, things work fine.