T
T
Tyoma Makeev2016-01-12 00:02:15
linux
Tyoma Makeev, 2016-01-12 00:02:15

How to make a good autostart program in Linux?

I decided to make myself a small environment to learn python. I created a virtual machine with ubuntu, installed pyenv, there is anaconda, well, actually IPython Notebook is in it.
I realized that I want the server with IPython Notebook to start when I boot the virtual machine, but at the same time, so that it is not in the background somewhere, but so that you can attach to it and see what it says, what errors displays if so.
Made myself a very crooked solution for this. Made automatic login in tty2 and added to the .bashrcfollowing lines:

if [ "$(tty)" = '/dev/tty2' ]; then tmux; fi
if [ -z "$(ps -e | grep 'jupyter-note')" ]; then [ -n "$TMUX" ] && command -v tmux >/dev/null && ./notebook.sh; fi

That is, the script first checks if it is in the tty2 terminal, opens tmux, then, if notebook is not yet running, but it is in tmux, it starts the script starting IPython Notebook.
The disadvantages of this solution are obvious, it will not be possible to use tmux if I suddenly want to deal with it at last, and in general it looks very crooked. Plus, what worries me even more is that when the virtual machine is turned off, IPython Notebook is cut off abruptly, not at all the way it should be.
I want to ask, what other good ways are there to do a similar thing, so that when I boot the computer, the program starts up, but at the same time so that I can somehow attach to it?
And are there any ways to normally complete it before turning off the computer, well, I mean, not manually?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir, 2016-01-12
@murmuringvoices

Supervisor: A Process Control System is a good tool for this. I recommend using it.

A
Arseniy, 2016-01-12
@CRImier

Screen, right?

M
Mikhail Beloshitsky, 2016-01-12
@mbeloshitsky

If it's correct, then depending on what kind of Ubuntu you have, you need to write either a unit to systemd or to upstart.
For upstart it would look something like this ( /etc/init/ipython-notebook.conf)

# Запуcкаем программу в tty2
#
# Дополнительно нужно убедиться, что на tty2 больше ничего не стартует 
# (хорошей идеей будет прибить /etc/init/tty2.conf)
start on stopped rc RUNLEVEL=[2345]

stop on runlevel [!2345]

respawn
exec /usr/bin/setsid sh -c 'exec notebook.sh <> /dev/tty2 >&0 2>&1'

Useful link unix.stackexchange.com/questions/170063/start-a-pr...

D
devel787, 2016-01-14
@devel787

> Создал виртуальную машинку с убунтой
Можно не выключать виртуальную машину, а приостанавливать.
И не нужно будет ничего запускать при загрузке виртуальной машины.
А в 1-й раз можно запустить всё нужное в tmux или screen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question