2

Ubuntu 14.04.5 LTS

Desired functionality: Remote Ubuntu server boots and automatically starts an ongoing process as a specified user. I can then log in remotely at a later time and view the past and current screen output of the ongoing process.

Current plan:

Automated steps:

  1. User "ubuntu" is logged in automatically to a terminal (TTY) on boot:

    "sudo nano /etc/init/tty1.conf"
    
    Change: exec /sbin/getty -8 38400 tty1
    
    To: exec /sbin/getty --autologin ubuntu -8 38400 tty1
    
  2. User "ubuntu" runs GNU screen:

    screen -d -m -S screenname
    
  3. User "ubuntu" runs the ongoing process within GNU screen:

    screen -S screenname -p 0 -X stuff "command^M"
    

Manual steps at later time:

  1. I log in remotely as user "ubuntu" in another terminal (TTY).
  2. I re-attach to the GNU screen session started in step 2:

    screen -r
    
  3. I am able to view past and current output of the process started in step 3.

Questions:

  • Is this the most effective way to achieve the desired outcome?
  • How can the commands in step 2 & 3 be automated?
  • How can the commands in step 2 & 3 not activate when I log in manually in step 4?
  • 1
    Will you be interacting with the attached terminal session, or merely monitoring it? Non-interactive sessions should output to log instead of tty. You can read the log in realtime over ssh. – user535733 Jan 7 '17 at 1:13
  • In this particulator use case, I will only be monitoring the terminal session output and terminating the process if necessary. However, I would like a solution that works for both monitoring and interacting in real time in case my use case changes in the future. – Toby Jan 7 '17 at 3:54
1

Answers (to my own questions)

  • Is this the most effective way to achieve the desired outcome?
    I am still unsure if the solutions below are the most effective, but I can confirm that it at least works on the stated version on Ubuntu.
  • How can the commands in step 2 & 3 be automated?
  • How can the commands in step 2 & 3 not activate when I log in manually in step 4?

    Step one
    I put the following at the bottom of the .profile file in the user directory of the user being automatically logged in during step 1 above (/home/ubuntu/.profile):

    pgrep processname || /home/ubuntu/processscript.sh
    

    When the user is automatically logged in, this command will execute automatically because it is in .profile. The first part of the command (before the "||" checks if 'processname' is already running. If not, it will execute the second part of the command (after the "||") i.e. "processscript.sh" ... If 'processname' is already running, it won't.

    This check was necessary as the process I wanted to run could not have multiple instances running at the same time. In addition, it solves the problem of process running a second time when I log in manually later.

    Step two
    Create a shell script file called "processscript.sh" with the following contents:

    #!/bin/bash
    screen -d -m -S screenname &&
    screen -S screenname -p 0 -X stuff "/home/ubuntu/command.sh^M"
    

    Ensure "processscript.sh" is in the /home/ubuntu directory, and that it is executable with the command:

    chmod 744 processscript.sh
    

    The above shell script creates a screen instance with the name "screenname", and if completed successfully, it stuffs the commandline of that screen instance with the command "command.sh" with a carriage return (which is what the "^M" at the end does).

    Step three
    In the previous step, I encountered some problems when stuffing a command with attributes to the screen commandline (I think some character escaping was required). To resolve this simply, I added the full command with attributes to a shell script and used that instead. Therefore, create another shell script (as above) called "command.sh" and add the full command in there.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.