Ask Your Question
1

how to run sage as a service

asked 2011-12-28 10:48:14 +0100

Gong-Yi Liao gravatar image

updated 2015-01-14 09:55:00 +0100

FrédéricC gravatar image

How do I run sage as a web application service (like running an instance of Ruby on Rails or a Django web application) without entering sage ipython CLI session?

Because I don't want to keep a terminal emulator like gnome terminal or x-term open,I just want to run sage in the background with log recorded in some files (say, $HOME/.local/sage/log/notebook.log).

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted
4

answered 2011-12-30 10:58:08 +0100

You can put the following command in your $HOME/.profile file.

sage -notebook

This file is executed every time you login.

If you are having multiple simultaneous logins, then you can put some checks whether sage is already running, e.g. using script like

if ps aux | grep 'sage'

then

echo "Sage already Running";

else

 sage -notebook

fi

edit flag offensive delete link more

Comments

Your grep might fail sometime, as it did for me. If some process are running which have sage in them, e.g. "message" then this fails. To avoid failure one could do a complex grep such as:

ps aux | grep sage | grep python | grep -v cleaner

The "cleaner" is added at the end to discount the "sage-cleaner" process which might be running even if the sage server is not running.

shivams gravatar imageshivams ( 2015-04-23 06:38:12 +0100 )edit
1

answered 2011-12-28 13:06:41 +0100

Emil Widmann gravatar image

updated 2011-12-28 13:10:27 +0100

There is the following in the Sage FAQ

QUESTION: How do I run sage in daemon mode, i.e. as a service?

ANSWER: We currently do not have a ready-to-go solution. There are several possibilities: Use screen, nohup or disown. We are tracking the issue at [http://trac.sagemath.org/sage_trac/ti...] - so stay tuned.

Hmm, this ticket is 5 years old, so I guess there is no full daemon mode available anywhere soon. But I guess you are satisfied with something simpler.

1) Make a startscript sage_bgd.sh (assuming $SAGE_ROOT is in your path, else use full path) which just contains:

sage -notebook

make it executable and klick on it in the GUI - the notebook server should start without terminal emulator.

2) Depending which Linux variant you use there should be alternative terminals available. E.g I can switch with Ctr-Alt-F1/F2/F3 between 3 different terminals. (On my system F1 has the default X session and F4 switches to the GUI, but that might be different for your system). But I can press Ctrl-Alt-F2, then log in with username/password, then start the sage notebook server (simplest with sage -notebook), and then go back with Ctrl-Alt-F4 to the GUI. The sage server is then running on tty2, I can use localhost:8000 in the X-session to log in to the notebook without any open terminal there.

there will be more sophisticated variants of course...

edit flag offensive delete link more

Comments

Hm - in the last Live CD I made the start script - option 1) - autoexec at system start (e.g. put the start script in /etc/profile.d), so it will be always there in the background. you can always check if it is running with the ps -a command. there should be 3 consecutive python processes. to stop: kill ID_OF_3d_PYTHONPROCESS

Emil Widmann gravatar imageEmil Widmann ( 2011-12-28 13:56:03 +0100 )edit
1

answered 2012-03-08 15:21:24 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Create the file /etc/init.d/sage_daemon (chmod 755) with the following content:

#!/bin/bash
# configuration
SAGE_HOME="/usr/local/install/sage-4.8"
SAGE_OPTS="-notebook interface='' secure=True"
SAGE_LOG="/var/log/sage/sage.log"
# commands
CMD_ECHO="/bin/echo"
CMD_GREP="/bin/grep"
CMD_SLEEP="/bin/sleep"
CMD_KILLALL="/usr/bin/killall"
sage_start() {
        $CMD_ECHO "Starting Sage..."
        $SAGE_HOME/sage $SAGE_OPTS >> $SAGE_LOG 2>&1 &
}
sage_stop() {
        $CMD_ECHO "Stopping Sage..."
        # CAUTION : maybe you kill something important in your server
        $CMD_KILLALL python
}
case $1 in
        start)
                sage_start
                ;;
        stop)
                sage_stop
                ;;
        restart)
                sage_stop
                $CMD_SLEEP 5
                sage_start
                ;;
        *)
                $CMD_ECHO "Use: $0 {start|stop|restart}"
                exit 1
                ;;
esac
exit 0

Configure $CMD_* according to your Unix/Linux environment (use "which" command to locate the right paths). In order to automatically start the service (Debian) when the server starts, you should run this command:

/etc/init.d# update-rc.d sage_daemon defaults 95

Hope it'd be useful for other people too.

edit flag offensive delete link more

Comments

Oh, I've just forgot the most important command: /etc/init.d/sage_daemon start, and you are ready to go.

Guille gravatar imageGuille ( 2012-03-08 15:26:13 +0100 )edit

The `stop` command can use some love in this script. The process ID of the notebook serving process gets saved (you can tell by the fact that trying to start the notebook twice gives you an error message). Something like SAGE_PID="/home/sage/public.sagenb/twistd.pid" kill `cat $SAGE_PID` would probably exit the notebook a little more cleanly (you'd have to look up the name and location of the pid file on your system, though)

nbruin gravatar imagenbruin ( 2012-09-04 19:50:47 +0100 )edit

Another thing is that the script should really run sage under a UID different from root. So, in sage_start you might want to do /bin/su - sageserver "$SAGE_HOME/sage $SAGE_OPTS >> $SAGE_LOG 2>&1 &" where sageserver is an appropriate user on your system.

nbruin gravatar imagenbruin ( 2012-09-04 19:58:29 +0100 )edit
0

answered 2014-07-23 17:39:41 +0100

Woodgnome gravatar image

While there's an accepted answer for this question it's also worth checking out http://ask.sagemath.org/question/2350...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-12-28 10:48:14 +0100

Seen: 3,255 times

Last updated: Jul 23 '14