Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two users; user1 and user2. I have a bash script run by user1, that needs to run a command on a screen terminal started by user2. Suggestions? Thanks Max

share|improve this question
up vote 3 down vote accepted

Do you want it to look as if user2 typed the command?

In the case of screen, you can use

screen -S user2/sessionname -p windowname -X stuff "~user1/somescript.sh
"

This will type the text ~user1/somescript.sh (referring to a file in user1's home directory) to a specific window of the given screen session and send a newline character.

(Yes, the second " must be in a new line, otherwise you would just send the text and have to press return in the screen session. Alternatively, use $'stuff blah\n')

To do this, you will need to have write access to user2/sessionname; read screen's manual page for description of multiuser and acladd.


In the generic "run stuff on another tty" case, you can use writevt /dev/pts/X sometext; the writevt tool is part of console-tools package. If your distro uses kbd instead, get writevt.c. This is specific to Linux.

Or do you just want user2 to see the output?

./somescript.sh >& /dev/pts/X

where /dev/pts/X is the tty device that belongs to user2's screen.

You will need write access; this can be given by user2 with chmod/setfacl, or you can use sudo.

(>& file is equivalent to > file 2>&1, which redirects both stdout and stderr to the given file.)

share|improve this answer
    
Thanks for the answer; to clarify, I need to accomplish the first goal (look as if user2 typed the command). What is the difference between screenname, and windowname? I looked through the man page for screen, but couldn't find anything. Thanks – Ben Mar 22 '11 at 23:02
    
@mazzzzz: A user may have multiple 'screen' sessions (I think I should have used sessionname rather than screenname). Each session can have multiple windows. (Having multiple windows displayed in a tiled layout is common: C-a :split) – grawity Mar 23 '11 at 0:19
    
And if you only have one window, what would its name be? Does it have a default name, or do I need to find it somewhere? – Ben Mar 23 '11 at 0:57
    
@mazzzzz: Just skip the -p option. – grawity Mar 23 '11 at 5:28
    
Don't know if this is the best practice, but it works :) Start the webhost user, then run a screen in there, then su into the server user, then run the server. Haha, there is something wrong with that. Thanks for the help (and if you have a better way to set it up, I'm all ears) – Ben Mar 26 '11 at 4:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .