Thursday, October 22, 2009

Running Scripts

In science it seems that we are supposed to somehow organically learn how to use linux computers. I don't know how people do this. I feel like I know only 15 commands because I use them everyday. Everything else I need to do on linux I either have to ask others to help me, or I look it up when I need it and then promptly forget. All books on the subject seem to give me more information than I need. Is there a 'Practical Linux for Dummies' book?

However, one of the MANY benefits of dating a computer scientist (for those who haven't tried it, I highly recommend it) is that Adam knows how to do almost anything I would want to do on a computer. I've lost track of how many times he has helped me.

Latest example of this is running a script remotely the need to stay logged into the computer. I know this is a basic task, but every time I've tried to get someone to show me how to do it, I've never gotten a clear-cut answer.

So this is how I did it (I am writing it here so that I when I promptly forget how to do this, it will be easy to remind myself):

1) At the top of your python code put the following line (after the import statements):
if __name__ == '__main__':
For more info.

2) Indent the rest of your code (as python requires you to do for all functions)

3) Make sure your code doesn't plotting in it.

4) Save your code as a python script (i.e. code2run.py)

5) In a terminal window type the following:
> python code2run.py code2run.out 2> code2run.err &

6) Check that your code is running by typing the command: top

7) Now you can close your terminal window and come back to check on the run later by typing "top" again.

8) Any outputs to the terminal will go to the file code2run.out, and error messages will go to code2run.err

It would be nice if I could include in this script something where it would email me or send me some sort of message once the run is complete. Anyone know how to do this?


1 comment:

  1. Hey Jess,

    That solution is bit overkill. You shouldn't need to modify your code just to do this. (Also, what if you want to do this for a program you don't have the source of?) Here are three solutions:

    1) When you run your program/script, put the word "nohup" in front of it, as in:

    % nohup myscript.py &

    This will start the program and run it in the background. The output will be written to a file (I don't remember its exact name, but it will be obvious). You can then close your terminal session and it will continue to run.

    2) There is a unix utility called "screen". I'll let you look up the details, but basically you start a "screen", then run whatever you want. Then you can close the screen, but it's still running. You haven't logged out of the shell. Then you can ssh from another computer and reconnect to that screen, and it will be as if you never logged out (because, as I say, you didn't). In that case, you don't have to worry about logging your output - it's still on the screen.

    3) Buy a VERY long power cable and video cable extender. Carry the screen with you.

    ReplyDelete