2

Basically, I'm working on a remote server, and connecting via SSH. Now, I can run processes / execute bash scripts normally with my SSH console, but there's one problem. As soon as I log off, those bash scripts just quit.

Is there any way for me to start a bash script, from SSH, and let it run, even if my computer, on which I initiated the bash script to the server, is switched off?

Thanks in advance, Peace! ~Tom

3 Answers 3

4

The nohup program jim suggests is good. See its command line options to change log location.

Another cool option is screen. When you start it you can have multiple screens and if you log off an later log in, then you can reattach to the same session with screen -r. It has many cool features but you need to read the doc.

The simplest thing is when you start some scrtips with & at end, you can use the disown bash command to make these scripts not terminate with bash exit.

Hope this gives you some clues.

Update, a quick getting started with screen:

  • type screen to start a session
  • you can immediately start commands
  • Ctrl+a+c will create a new window and you go to it, you can start other ocmmands
  • Ctrl+a+# where # is from 0 to 9 will move you to the window you select
  • Ctrl+a+d detach your session and you are back to normal terminal
  • screen -r re-attaches you to a session, if you have multiple, you are asked to type part of its name (must be a unique sequence contained in the session name)
  • .screenrc can be used for goodies like scrollback size, key bindings, terminal options, etc. See these two example files for nice options: one and two.
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and comprehensive answer. Gives all possible options for the question.
2

Test script

script call it remote.sh:

at now <<!
/path/to/test.h> /tmp/test.out
!

copy this to the remote box: scp remote.sh thomas@remote: ssh remote 'chmod +x remote.sh'

  1. write the commands you want in a copy of test.sh locally, example:

cd /tmp; cp *.foo /path/to/somewhere/else

  1. copy the script to the remote server

    scp test.sh thomas@remote: ssh remote 'chmod +x test.sh'

  2. ssh remote './remote.sh'

Repeat steps 1, 2 & 3 each time you need to run your script without waiting for it.

4 Comments

You'll have to excuse me. I'm an extreme amateur. Do you think you could give me some more detail? Thanks.
I'm not sure that does what you want. Skip over it for a minute.
I could really use some sort of command that enables me to execute bash scripts without my computer having to be on/logged into the console.
Can you give me an example of what you mean - if your computer is not on how can you tell it what to do? The at command takes a time parameter, so you can tell it to wait for as long as you want. If you want something to run automatically at the same time or day of the week, create a crontab entry. If you give more information maybe one of us can help.
1

In my opinion one of the easiest ways to achieve this is to use 'screen'. This will create a virtual terminal which won't kill your tasks after you logout.

  1. Login into server
  2. Start screen (just type screen)
  3. Start Your Command
  4. Logout

Your process will now continue to work.

After you relogin type 'screen -list' and find the pid of the last screen session. Then type

screen -dr YOUR_PID

To reopen the screen and continue your work.

P.S. Screen is full of features that are super usefull for remote administration. Just have a look at a tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.