Autostart

A common use case for map2 is to run a script automatically after logging in, the way to do it depends on your Linux distro and system setup.

We’ll use Systemd since most distros ship with it, but changing the commands to work on other systems should be pretty easy as well.

Setting things up

The way to prepare things depends on how you are running your map2 scripts, please check the Secure setup section.

If you are running your scripts as superuser as shown in the Getting started section, please first complete any of the ways in Secure setup and continue with the appropriate section below.

The lazy way

Uset this method if you are using:

Copy the service definition into ~/.config/systemd/user/map2.service.

[Unit]
Description=map2 autostart service
PartOf=graphical-session.target

[Service]
ExecStart=python /path/to/my-map2-script.py
Restart=always
RestartSec=5s

[Install]
WantedBy=graphical-session.target

And change /path/to/my-map2-script.py to your script path.

The secure way

Uset this method if you are using:

In the following section, replace /home/map2/my-map2-script.py with your script path.

Create a shell script file in /home/map2/autostart.sh:

#!/bin/bash
# map 2 autostart script
# runs a map2 script by switching to the map2 user

su map2 -pc 'python /home/map2/my-map2-script.py'

And run the following commands:

# make the autostart script executable
chmod +x /home/map2/autostart.sh

# allow everyone to run the autostart script
echo "ALL ALL=(root) NOPASSWD:SETENV: /home/map2/autostart.sh" | sudo tee -a /etc/sudoers

Copy the following into ~/.config/systemd/user/map2.service:

[Unit]
Description=map2 autostart service
PartOf=graphical-session.target

[Service]
ExecStart=sudo -E /home/map2/autostart.sh
Restart=always
RestartSec=5s

[Install]
WantedBy=graphical-session.target

Running the service

Now that we created a service, we need to make sure it works and enable it so it starts automatically.

# tell systemd we edited a service
systemctl --user daemon-reload

# start the service and make sure it runs the script
systemctl --user start map2

# after ensuring it works, enable it so it runs on every startup
systemctl --user enable map2

Your script should now run automatically when you login to your desktop environment.