Hudson-CI launched screen session terminates when task ends - macos

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.

Related

Startup script to start a detached screen and run a bash script inside

I have an issue with a startup script I've written and can't for the life of me figure out what's wrong. In essence, I want to start a detached screen, pass a daemon start command into the screen, then change directory and run "npm start" at the directory I've changed to.
Preferably, I'd like to install the screen script as a service, but I've had no success. Screens won't start at all. I've screen -list and even sudo screen -list while the service was installed. I've tried Type=forking Type=oneshot and still nothing.
Crontab is the only thing that has somewhat so far.
I've gotten the screen to stay open, however when I reattach to the screen I get the error:
/path/to/script.sh : 6: /path/to/script: npm: not found.
I used nvm to install node and npm. I tried linking to /usr/bin or /usr/local/bin with no success. Either errors with too many links, or node not found, even though its been linked.
For the scripts I have, the main script which starts the screens is
#!/bin/sh
SCRIPT_DIR=/path/to/script/folder/in/user/directory
screen -dmS nameofscreen bash -c $SCRIPT_DIR/script_to_run.sh
and the script that is supposed to be run is as follows:
#!/bin/sh
USR_BIN=/usr/local/bin
$USR_BIN/daemon_to_start -arg1 -arg2 &&
sleep 2
cd /path/to/npm/app/folder && npm start
sleep 3
exec $SHELL
Once again, with these scripts, the screen starts and stays open, however npm not found error is thrown. Also, if I use the absolute directory for npm, node not found becomes the new error. I've spent three days on this and never wrote a script. I'm starting to lose my mind.
PLEASE HELP!
EDIT: With the help of lojza and adding the node binary to my PATH, It somewhat works! Now when the startup script runs, only one screen is started. The script is supposed to start 4 screens. I've tried appending & and even && to the end with no luck. I will continue searching.
Call npm with full path:
/path/to/npm/app/folder/npm start
/usr/local/bin/npm start #in my case
or
cd /path/to/npm/app/folder && ./npm start

How to run screen using bash script

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

Run screen through crontab

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

Gnu Screen hardcopy without joining screen

I writing a python app that runs some commands on a gnu screen without joining the screen and seeing the hardcopy buffer. In particular, I am running:
screen -dmS test
screen -S test -p 0 -X stuff "ls$(printf \\r)"
screen -S test -X hardcopy screenOutput.txt
when I look at screenOutput.txt, I see nothing.
However, if I join the screen, and then run hardcopy,
screen -dmS test
screen -S test -p 0 -X stuff "ls$(printf \\r)"
screen -r test
(quit the screen with c-A c-D)
screen -S test -X hardcopy screenOutput.txt
Then I see the output in screenOutput.txt.
Is there a way to get hardcopy to write to the file, without joining the screen?
See linux - Send command to detached screen and get the output - Unix & Linux Stack Exchange for an example on how to send commands to a session. One correct invocation is:
screen -dmS test
screen -S test -X hardcopy screenOutput.txt
I.e. -X shall be the first command option. -d/-r switches to search only attached/detached sessions have to follow it (the docs are notoriously vague on this); in this case, they are not needed at all.
I confirmed this to not work (produce blank file) in screen v4.0.3 and work in v4.2.0 and up in the same environment.
Extensive investigation:
Debugging shows that the root cause is in WriteFile at fileio.c:472 : if (!fore) break; which quits the function without writing anything because fore (a pointer to the foreground window) is indeed NULL.
I couldn't pinpoint the specific commit where this was fixed, but did check that it isn't NULL in v4.2.0. The variable is set in a number of places around the codebase and is reset to NULL in roughly the same amount of places, often in code that follows the assignment. So the chances of a working workaround are very slim.
I hereby reaffirm that in screen v4.0.3, hardcopy is broken and you have to upgrade (e.g. install a version to /usr/local so that it overrides the stock one).

Pass command from scheduled script to program running in xterm window

I have a game server running in an xterm window.
Once daily I'd like to send a warning message to any players followed after a delay by the stop command to the program inside the xterm window from a script running on a schedule. This causes the cleanup and save functions to run automatically.
Once the program shuts down I can bring it back up easily but I don't know how to send the warning and stop commands first.
Typed directly into xterm the commands are:
broadcast Reboot in 2 minutes
(followed by a 2 minute wait and then simply):
stop
no / or other characters required.
Any help?
Do you also need to type something from the xterm itself (from time to time) or do you want your server to be fully driven from external commands?
Is your program line-oriented? You may first try something like:
mkfifo /tmp/f
tail -f /tmp/f | myprogram
and then try to send commands to your program (from another xterm) with
echo "mycommand" > /tmp/f
You may also consider using netcat for turning your program to a server:
Turn simple C program into server using netcat
http://lifehacker.com/202271/roll-your-own-servers-with-netcat
http://nc110.sourceforge.net/
Then you could write a shell script for sending the required commands to your "server".
If you know a little about C programming; I remember having once hacked the script program (which was very easy to do: code is short) in order to launch another program but to read commands from a FIFO file (then again a shell script would be easy to write for driving your program).
Something you might try is running your program inside a screen session.
You can then send commands to the session from cron that will be just
as if you typed them.
In your xterm, before launching the program do:
screen -S myscreen bash
(or you can even replace bash by your program). Then from your cron
screen -S myscreen -X stuff 'broadcast Reboot in 2 minutes\n'
sleep 120
screen -S myscreen -X stuff 'stop\n'
will enter that text. You can exit the session using screen -S myscreen -X quit
or by typing ctrl-a \.
screen is intended to be transparent. To easily see you are inside screen, you can
configure a permanent status bar at the bottom of your xterm:
echo 'hardstatus alwayslastline' >~/.screenrc
Now when you run screen you should see a reverse video bottom line. Depending
on your OS it may be empty.

Resources