Help with creating startup scripts using screen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with creating startup scripts using screen
# 1  
Old 10-14-2012
Help with creating startup scripts using screen

Edit: So sorry! I really didn't do a good job of clearly stating what I needed. Going to completely rewrite my post so everything is clearly stated.

I'm having trouble writing a shell startup script for a linux server. It uses the bash shell if I remember correctly. I'm trying to write it so that the script created a screen session named 'main' that has 3 screens. The problem is, I have no idea how to do that. Also, one of the screens runs a command that needs the user to input a '0' and then press return, I was wondering if that can be done automatically (with piping?). Here's what I would like it to do in detail:

1. Create a screen session with the name 'main' (I think that uses the -S flag)
2. Attach to the session (perhaps already done in item 1?) and run this line:
Code:
cd /home && ./start.sh

3. Create a second screen (in the same session) and run this line:
Code:
cd /home && ./teamspeak.sh

4. Create a third and final screen and run this line (This is the command that also needs to take in the 0 and return after it is run):
Code:
cd /root/ShoutCast-server && ./sc_serv

5. Detach from the screen session

I would like the screens to have started not directly from the lines of code(I.E. run screen command, then run the line of code in the screen). This is because those programs are constantly running, if anything happens I would like for people to be able to just go back into that screen, kill the command and re-run it; without having to make the screen again.

Any help would be greatly appreciated!

Last edited by Pyitoechito; 10-15-2012 at 09:42 AM.. Reason: Revising, organizing everything
# 2  
Old 10-16-2012
You could just kill it with a user signal caught by a 'trap', which kills the command and exit's with an odd code. If the parent senses that exit code, it reruns the last command. There are lots of things you can do for ipc from other sessions if you wrote the daemon scripts.

You might just rerun it every time it fails and just kill it.
# 3  
Old 10-16-2012
You say Linux, have you considered using tmux? It's superior to screen in everything I've ever used it for. I've actually got my tmuxrc mapped to work more similarly to the binds I've used from screen in the past that you can try out, examples at the bottom for starting up new windows/screens as well:

Code:
# Make it use C-a, similar to screen..
unbind C-b
unbind l
set -g prefix C-a
bind-key C-a last-window

# Default Reload key
bind r source-file ~/.tmux.conf
unbind n
bind n command-prompt "new-session -d -s %%"
# Unsure, but I like VI...
setw -g mode-keys vi
# Make it so I can click on panes and get expected behavior...
#setw -g mode-mouse off
#set-option -g mouse-select-pane on

# Navigation Binds
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind q previous-window
bind w next-window
# Start counting windows at 1
set -g base-index 1
# Rebind the window splits
unbind %
bind - split-window
bind \ split-window -h

# Rebind rename
unbind .
bind r command-prompt "rename-window %%"
# Also, let's just get a basic command prompt for debugging...
bind . command-prompt
# Copy/Paste - Make it VI style.
unbind p
unbind y
bind y copy-mode
bind p paste-buffer -s \015
# Change the default time messages are displayed to 4 seconds
set-option -g display-time 4000

# Sane scrolling
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

# If you don't set the below you might not be able to use bold colors... Really                                                                                              weird.
# xterm-256 is a mistake.
set -g default-terminal "xterm"
set -g history-limit 10000

# Visual - Still don't know how to do the 256 range of colors...
set -g status-bg black
set -g status-fg white
set -g status-interval 60
set -g status-left-length 30
set -g status-left '#[fg=orange](#S) #[default]'
set -g status-right '#[fg=white]#(hostname): #(ifconfig eth0|head -2| tail -1 | sed "s/^[\ ]*//g" | cut -d " " -f2)#[default] #[fg=cyan]Time: %H:%M#[default]'
set -g visual-activity on
set -g visual-bell off
setw -g monitor-activity on
setw -g window-status-current-fg white
setw -g window-status-current-fg blue
setw -g window-status-current-bg black
setw -g window-status-current-attr underscore
setw -g automatic-rename on
set -g pane-active-border-fg white
set -g set-titles on
set -g set-titles-string '#S:#I.#P #W'

new-session -d -s Consoles
new-session -d -s STIG -n 'AIX' 'ssh dc6nim'
neww -n 'SUN' 'ssh jmpstart'
neww -n 'LNX' 'ssh -t -t -L8081:localhost:10000 dc6nim "ssh -L10000:localhost:8080 10.160.1.125"'

Here's the code for starting new windows:

Code:
new-session -d -s Consoles
new-session -d -s STIG -n 'AIX' 'ssh dc6nim'
neww -n 'SUN' 'ssh jmpstart'
neww -n 'LNX' 'ssh -t -t -L8081:localhost:10000 dc6nim "ssh -L10000:localhost:8080 10.160.1.125"'

This User Gave Thanks to Vryali For This Post:
# 4  
Old 10-16-2012
Right now the linux server doesn't have tmux, and I don't really feel like adding it just to set up one session. It's a linux server for running Minecraft, the screens are the minecraft server, a teamspeak server, and a shoutcast server. One per window.

I have made progress on the shell script (I think):
Code:
#!/bin/bash

# shortcut variable
SCR="screen -S test -t"
# start screen session 'test' with window 'vim'
screen -d -m -S test -t vim
# run a command in window 'vim'
$SCR vim -X stuff $'vim \riDone\rwith\rthis.\x1b'
# make new window 'emacs'
$SCR vim -X screen -t emacs
# run command in window 'emacs'
$SCR emacs -X stuff $'emacs \rHello, Sir!\r'
# new window 'pico'
$SCR emacs -X screen -t pico
$SCR pico -X stuff $'pico \rDo your homework!!!'
# unset variable
unset SCR

Right now that code makes the session and windows with the proper names, but doesn't run the "stuff"s. Attaching to the screen windows shows only the shell input line and not the editors.

Last edited by Pyitoechito; 10-16-2012 at 07:28 PM..
# 5  
Old 10-17-2012
A more complete set of requirements might help us understand why you need 3 screens?
# 6  
Old 10-17-2012
The main reason for the screens is that the server needs to have programs that are always running. So the first screen runs the Minecraft server itself, the second screen runs the Team Speak server, and the third screen runs the SHOUTcast server.

The screens are so people logged into the linux shell can actually do things while the commands for running the server are still running. The original startup scripts (which are still there) start up minecraft and team speak on separate, unnamed screen sessions. It was a guessing game of which screen to attach to. So I set it up so that they all run on one screen manually. But if the linux server itself ever goes down and starts up again, it will go back to separate sessions due to the old startup scripts.

The reason I want to start a screen, then run the command is so that if, for example, the Minecraft server crashes. Then one needs simply to log into the linux server, attach to the screen session (screen -x, it should be the only session) and then end the crashed server and re-run the server start script. If the screen was started with the Minecraft start scripts passed in with it, then ending the Minecraft server would terminate the screen. Some of the people who manage the server are not really skilled with unix (I'm still learning, myself) so it might confuse them for the screen to terminate on them, and have to figure out how to create a new one.

The script I posted above was just a test to see if I could get those screens started and get commands running in them. But I only managed to create all 3 properly named screens with it.

Last edited by Pyitoechito; 10-17-2012 at 01:12 PM.. Reason: Added more info
# 7  
Old 10-17-2012
It's a very weird model, as server daemons usually log to flat files in text, sometimes via syslog(), and take requests on tcp/ip, named pipes or message queues (mmap()'d files would work, too). You could run each on a Xnc desktop and connect with vncviewer as necessary. What is your security model?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Solaris 10 startup scripts

Hi all, I have a server in maintenance mode and need to boot it up. Its due to a broken service "RepX". I need to stop the service from trying to start at boot up but i can't find where it is booting up from... it is not in any of the rcX.d directories and the two locations i have found it... (6 Replies)
Discussion started by: Tommyk
6 Replies

2. UNIX for Advanced & Expert Users

xsession startup scripts

Can someone please tell me how to run xsession startup scripts whenever I start or after I restart my X session? I'm not trying to do anything fancy I just want this to run. This makes life a lot easier when I use a dual monitor. xrandr --output VGA1 --mode 1024x768 --rate 60 (7 Replies)
Discussion started by: cokedude
7 Replies

3. AIX

Startup/shutdown scripts in AIX

hi, If we place Sxx (startup script) and Kxx(shutdown script) in /etc/rc.d/rc2.d,then it would start and stop automatically(assume they are linked to other script that actually starts/stops). is there really a link needed here to /etc/rc.d/init.d? if not,what is the use of this directory..?... (1 Reply)
Discussion started by: to_bsr
1 Replies

4. UNIX for Dummies Questions & Answers

Startup/Kill Scripts

Hello, I have to perform an audit of a system at work and I am looking at its /etc/rc3.d: K01tog-pegasus K74nscd S08iptables S50openemm K01yum K74ntpd S09isdn S55cups K02NetworkManager K85mdmpd S09pcmcia S55sshd K03rhnsd ... (8 Replies)
Discussion started by: mojoman
8 Replies

5. AIX

Startup rc scripts log

Hi, I am trying to start services on system reboot on AIX. Have put the S* links under rd2.d & K* links under other rc*.d At the moment, all scripts are getting called. However, the services aren't coming up. Where can i find the logs for these to check what failed. (8 Replies)
Discussion started by: vibhor_agarwali
8 Replies

6. HP-UX

Startup scripts

Hi all We have HP UX 11.23 installed on 4 RISC servers (2 oracle databases, 2 Oracle App Servers) , we are in a construction period , so the power failure may happen more than once a day. I need to learn how to create an automatic startup services as in Windows, if we know that the services... (5 Replies)
Discussion started by: kafaween
5 Replies

7. AIX

Creating startup service for JBoss

Hello Friends, Does anyone know how to create a startup script for Jboss on IBM AIX 5.3? Please help me, I'd be highly grateful to you... Thanks & Regards, Vinit (0 Replies)
Discussion started by: vpatil6688
0 Replies

8. Linux

startup scripts (rc3.d)

I've created the following link in order to startup apache tomcat on startup, however, it does not seem to run. Am I missing something out? :confused: /etc/init.d lrwxrwxrwx 1 root root 16 Sep 5 14:59 K73ypbind -> ../init.d/ypbind lrwxrwxrwx 1 root root 16 Sep 11 13:09 S100tomcat ->... (5 Replies)
Discussion started by: jon80
5 Replies

9. AIX

Startup/Shutdown scripts

I understand that by putting in entries into the /etc/inittab file. We can actually call the our scripts during startup. mkitab "start_server:2:once:sh /scripts/startserver.sh" Would the system wait for startserver.sh finish executing before it goes to another entry? and how long would it... (1 Reply)
Discussion started by: vincente
1 Replies
Login or Register to Ask a Question