#!/bin/bash # # Runs a command in a screen session, or re-attaches to such a screen session if # it already exists. # main () { force=0 screen_id="" while getopts ":hvfi:" option; do case $option in h) usage 0 ;; v) echo "scrot - screen run (once)" echo "(C) 2006, Matthew Gates " echo "Distributed under the terms of the GNU GPL (v2)" exit 0 ;; f) force=1 ;; i) screen_id="$OPTARG" ;; *) echo "ERROR: unknown option: $option" 2>&1 ;; esac done shift $(($OPTIND - 1)) if [ $# -lt 1 ]; then usage 1 fi if [ "$screen_id" = "" ]; then screen_id=scrot$1 fi session=$(screen -ls |grep \.$screen_id 2>&1) session_id=$(echo "$session" |awk '{ print $1 }') session_state=$(echo "$session" |awk '{ print $2 }') session_count=$(echo "$session" |grep -v '^ *$' |wc -l) case $session_count in 0) start_prog $@ ;; 1) try_reconnect $@ ;; *) multiple_sessions $@ ;; esac } start_prog () { echo "Starting new VLC session in screen" # sleep 1 screen -S $screen_id $@ } try_reconnect () { case "$session_state" in \(Detached\)) echo "$1 session is detached, re-attaching" # sleep 1 screen -r $session_id ;; \(Attached\)) if [ $force -eq 1 ]; then echo "forcing dis-connection of other screen session..." pid=$(echo $session_id |cut -d . -f 1) kill -HUP $pid # sleep 1 echo "attaching to session..." screen -r $session_id exit 0 else echo "Session is already attached to screen session $session_id" echo "Use -f option to force connection here." exit 1 fi ;; *) echo "ERROR: no session found despite it looking like one!" 1>&2 exit 127 ;; esac } multiple_sessions () { echo "It seems id $1 is already running in more than one session..." echo "You should connect to one using \"screen -r [id]\" and kill it" screen -ls |grep \.$screen_id 2>&1 exit 2 } usage () { echo "Usage:" echo " scrot [-f] [-i id] command ..." echo "" exit ${1:-0} } main $@ exit 0 __END__ =head1 NAME scrot - SCreen Run One Time =head1 SYNOPSIS scrot [options] program [args] =head1 DESCRIPTION scrot runs a program in a screen session one time only. The first time a program is run it starts a new session, subsequent calls to scrot with the program name (or ID - see option B<-i>) connect the calling terminal to the existing screen session. This is useful if you use a terminal based IRC client/mail client/audio player, and want to take it over on the console when you X session needs to be re-started, or you lose the terminal for whatever reason. Each program is run with an ID which is the item which is kept unique. By default the ID is the program name with no arguments, but can be specified using the B<-i> option. =head1 OPTIONS =over =item B<-f> Force the current terminal to grab existing sessions. Without this option, if an existing session with the same ID exists the normal behavior is to warn the user that one with the ID exists. Using this option will force scrot to take over the session. =item B<-h> Print the command line syntax an option details. =item B<-i> I Specify the ID of the program to run. If this option is not specified, the program name will be used. =item B<-v> Print the program description and version. =back =head1 ENVIRONMENT N/A =head1 FILES N/A =head1 EXAMPLES I use scrot to run the IRC client IRSSI. I do this with the alias irc, defined as follows (bash alias syntax): alias irc='scrot -f irssi' This simply calls irssi (my IRC client) with scrot making sure there is only one instance at any one time. If there is an existing session in another terminal, the B<-f> option forces disconnection from that terminal. This way I can switch IRC between console and xterm sessions without having to quit IRC. :) =head1 LICENSE scrot is released under the GNU GPL (version 2, June 1991). A copy of the license should have been provided in the distribution of the software in a file called "LICENSE". If you can't find this, then try here: http://www.gnu.org/copyleft/gpl.html =head1 AUTHOR Matthew Gates http://porpoisehead.net/ =head1 CHANGELOG =over =item Date:2006-06-01 Created, Author MNG Original version. =item Date:2006-08-22 Documented, MNG Added this cruft. =back =head1 BUGS Please report bugs to the author. =head1 SEE ALSO screen(1). =cut