Project Initialization Scripts
Harald Schilly • • cocalc
Under the hood, CoCalc provides you with a very flexible and versatile Linux environment. Due to the upgrade to Kubernetes, our (unofficial) support for using crontab files for periodic tasks was removed. Instead, there is a new and more flexible mechanism to use regular Bash, Python, or SageMath files to accomplish even more.
Project Initialization
When a CoCalc project starts up, an instance of Supervisor is started and responsible for running:
- an instance of “local hub”. It is used for managing the project, communication with the outside world, monitoring, etc.
- sshd: the endpoint for remote SSH access
- initialization file: it sit inside your project and is only started if it exists.
It is located in your home directory and named
project_init.sh
. As the name suggests, it is run via the Bash interpreter.
Initialization file management
Supervisor is configured to run this initialization file like background processes.
This means it checks the exit value of the process and restarts it if this code is not 0
or 2
.
Therefore, a script that runs without an error is not restarted.
On the other hand, if the process is terminated, interrupted or crashes – therefore the exit code is e.g. 1
– the process is restarted.
This adds some robustness to long running background jobs.
Example 1: record start time
A very simple example is to record the project’s start time.
Go to your home directory and create a file project_init.sh
with that content:
This is a very simple bash script, which pipes the output of the date
command into the file project-start
.
In order to see its effect, the file needs to be saved and the project restarted. Give it a few seconds to come back online and run the script. After that (maybe click the refresh button in the file listing) you should see this file and its content might look like:
Other languages besides Bash?
You can run any language via bash’s exec
!
For example, project_init.sh
containing
will run a Python 3 initialization file named project_init.py
.
Example 2: a periodic task in Python
Here we write a small Python script,
which runs an infinite loop (make sure to use time.sleep
!) and evaluates a function every 5 minutes.
This examples uses the library schedule.
Feel free to choose any other solution.
project_init.sh
: containsexec python3 project_init.py
- The content of
project_init.py
is:
Indeed, after restarting the project the output of ps auf
shows this “daemon” task as a child of supervisor:
and the output file task_output.log
contains:
Example 3: Periodic task in SageMath
run.sage
is similar to the Python script above.
project_init.sh
:exec sage run.sage
- This results in Sage running a small task every two minutes and appends outputs to
sage_output.log
:
Debugging
To figure out why a script doesn’t work as it should, there are two ways to debug it:
- Run it directly in a terminal (create a
*.term
file) and runbash project_init.sh
orpython3 project_init.py
. - Check the logfile of Supervisor by running this in a terminal:
cat /tmp/.cocalc/supervisord.log
. Among its logging there are likely entries hinting for exit states (e.g.INFO exited: project_init_sh (exit status 0; expected)
) or they showstdout
/stderr
output of the failed commands. - A common pitfall is to assume
~/.bashrc
is run. Since this is a non-interactive session, you need to explicitly source any additional environment information.