ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 23 Apr 2015 06:38:12 +0200how to run sage as a servicehttps://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/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). Wed, 28 Dec 2011 10:48:14 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/Answer by Woodgnome for <p>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? </p>
<p>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). </p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=23553#post-id-23553While there's an accepted answer for this question it's also worth checking out http://ask.sagemath.org/question/23509/how-to-run-sage-as-a-daemon-service/Wed, 23 Jul 2014 17:39:41 +0200https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=23553#post-id-23553Answer by Emil Widmann for <p>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? </p>
<p>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). </p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13074#post-id-13074There 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/ticket/381]
> - 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...
Wed, 28 Dec 2011 13:06:41 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13074#post-id-13074Comment by Emil Widmann for <p>There is the following in the Sage FAQ</p>
<blockquote>
<p>QUESTION: How do I run sage in daemon mode, i.e. as a service?</p>
<p>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
[<a href="http://trac.sagemath.org/sage_trac/ticket/381">http://trac.sagemath.org/sage_trac/ti...</a>]
- so stay tuned.</p>
</blockquote>
<p>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.</p>
<p>1) Make a startscript sage_bgd.sh (assuming $SAGE_ROOT is in your path, else use full path) which just contains:</p>
<pre><code>sage -notebook
</code></pre>
<p>make it executable and klick on it in the GUI - the notebook server should start without terminal emulator.</p>
<p>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.</p>
<p>there will be more sophisticated variants of course...</p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=20619#post-id-20619Hm - 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_PYTHONPROCESSWed, 28 Dec 2011 13:56:03 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=20619#post-id-20619Answer by Guille for <p>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? </p>
<p>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). </p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13342#post-id-13342Create 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.Thu, 08 Mar 2012 15:21:24 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13342#post-id-13342Comment by Guille for <p>Create the file /etc/init.d/sage_daemon (chmod 755) with the following content:</p>
<pre><code>#!/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
</code></pre>
<p>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:</p>
<p>/etc/init.d# update-rc.d sage_daemon defaults 95</p>
<p>Hope it'd be useful for other people too.</p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=20157#post-id-20157Oh, I've just forgot the most important command: /etc/init.d/sage_daemon start, and you are ready to go.Thu, 08 Mar 2012 15:26:13 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=20157#post-id-20157Comment by nbruin for <p>Create the file /etc/init.d/sage_daemon (chmod 755) with the following content:</p>
<pre><code>#!/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
</code></pre>
<p>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:</p>
<p>/etc/init.d# update-rc.d sage_daemon defaults 95</p>
<p>Hope it'd be useful for other people too.</p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=19114#post-id-19114Another 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.
Tue, 04 Sep 2012 19:58:29 +0200https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=19114#post-id-19114Comment by nbruin for <p>Create the file /etc/init.d/sage_daemon (chmod 755) with the following content:</p>
<pre><code>#!/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
</code></pre>
<p>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:</p>
<p>/etc/init.d# update-rc.d sage_daemon defaults 95</p>
<p>Hope it'd be useful for other people too.</p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=19115#post-id-19115The `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)
Tue, 04 Sep 2012 19:50:47 +0200https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=19115#post-id-19115Answer by VInay Wagh for <p>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? </p>
<p>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). </p>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13071#post-id-13071You 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
Fri, 30 Dec 2011 10:58:08 +0100https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?answer=13071#post-id-13071Comment by shivams for <p>You can put the following command in your $HOME/.profile file. </p>
<blockquote>
<p>sage -notebook</p>
</blockquote>
<p>This file is executed every time you login. </p>
<p>If you are having multiple simultaneous logins, then you can put some checks whether sage is already running, e.g. using script like</p>
<blockquote>
<p>if ps aux | grep 'sage'</p>
<p>then</p>
<pre><code>echo "Sage already Running";
</code></pre>
<p>else</p>
<pre><code> sage -notebook
</code></pre>
<p>fi</p>
</blockquote>
https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=26627#post-id-26627Your 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.Thu, 23 Apr 2015 06:38:12 +0200https://ask.sagemath.org/question/8598/how-to-run-sage-as-a-service/?comment=26627#post-id-26627