Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor II
1,533 Views

How to run Python file on startup on Intel Galileo

Jump to solution

How do I run a python file on startup on the Intel Galileo.

Tags (1)

Accepted Solutions
Highlighted
Employee
18 Views

Hello BrechtW,

On the eglibc image for Galileo you can do it with a system service, I'll give you a brief example on how to do it. You will need three files, your script (test.py), a shell script (test.sh) and your service (test.service).

test.py will be the following simple script:

import time

x = 0

f = open('/home/root/test.txt','w')

while x < 10:

print x

x += 1

f.write(str(x)+"\n")

time.sleep(1)

test.sh will simply start test.py (I had some issues trying to make the service and I was able to solve them this way):

# !/bin/sh

/usr/bin/python /home/root/test.py

test.service will look like this:

# !/bin/sh

[Unit]

Description=test service

[Service]

ExecStart=/home/root/test.sh

Type=idle

[Install]

WantedBy=basic.target

Give test.sh executable right with chmod +x test.sh and store test.service in /lib/systemd/system, test.py and test.sh can be store anywhere but on my example they are stored in /home/root. Once the service file is store in the appropriate directory you can enable the service with systemctl enable test.service. Your script should now start on boot.

Peter.

View solution in original post

8 Replies
Highlighted
Employee
19 Views

Hello BrechtW,

On the eglibc image for Galileo you can do it with a system service, I'll give you a brief example on how to do it. You will need three files, your script (test.py), a shell script (test.sh) and your service (test.service).

test.py will be the following simple script:

import time

x = 0

f = open('/home/root/test.txt','w')

while x < 10:

print x

x += 1

f.write(str(x)+"\n")

time.sleep(1)

test.sh will simply start test.py (I had some issues trying to make the service and I was able to solve them this way):

# !/bin/sh

/usr/bin/python /home/root/test.py

test.service will look like this:

# !/bin/sh

[Unit]

Description=test service

[Service]

ExecStart=/home/root/test.sh

Type=idle

[Install]

WantedBy=basic.target

Give test.sh executable right with chmod +x test.sh and store test.service in /lib/systemd/system, test.py and test.sh can be store anywhere but on my example they are stored in /home/root. Once the service file is store in the appropriate directory you can enable the service with systemctl enable test.service. Your script should now start on boot.

Peter.

View solution in original post

Highlighted
Honored Contributor I
18 Views

I've followed the suggestion at of using the iotkit-agent.service to set a specific IP (I know there is a way of doing it with connman, but I wanted to use ifconfig...) so:

# cd /home/root/

root@galileo:~# cat fixedip

# !/bin/sh

/sbin/ifconfig enp0s20f6:avahi netmask 255.255.0.0 up

# cd /lib/systemd/system

root@galileo:/lib/systemd/system# cat fixedip.service

[Unit]

Description=fixedip

After=network.target

[Service]

ExecStart=/home/root/fixedip

Type=idle

[Install]

WantedBy=multi-user.target

root@galileo:/lib/systemd/system# systemctl enable fixedip.service

Created symlink from /etc/systemd/system/multi-user.target.wants/fixedip.service to

/lib/systemd/system/fixedip.service.

And that's it, the IP is set at startup.

HTH

Fernando.

Highlighted
New Contributor II
18 Views

@Intel_Peter How do I set the priority and in which file?

My guess is putting this in test.sh.

nice -n -20 python /my_folder/my_program.py &

Is this correct?

0 Kudos
Highlighted
Employee
18 Views

What do you mean by priority? On boot? Or would you like to change the process niceness once the OS has finished booting?

Peter.

0 Kudos
Highlighted
New Contributor II
18 Views

My program that runs my robot should have the highest priority. I think this is the niceness.

0 Kudos
Highlighted
Employee
18 Views

Indeed you are correct, a process niceness determines its priority. If you wish to change the process' niceness on boot you can do it by changing the test.sh script (which I posted above). The script should now look like this:

# !/bin/sh

nice -n -20 /usr/bin/python /home/root/test.py

That should change your script niceness on boot.

Peter.

0 Kudos
Highlighted
New Contributor II
18 Views

@Intel_Peter I tried your answer but without succes:

galileo login: root

Password:

root@galileo:~# cd /home/root/

root@galileo:~# ls

index.html miniconda3 test.py test.sh

root@galileo:~# cat test.py

import time

x = 0

f = open('/home/root/test.txt','w')

while x < 10:

print x

x += 1

f.write(str(x)+"\n")

time.sleep(1)

root@galileo:~# cat test.sh

# !/bin/sh

/usr/bin/python /home/root/test.py

root@galileo:~# cat /lib/systemd/system/test.service

# ! /bin/sh

[Unit]

Description=test service

[Service]

ExecStart=/home/root/test.sh

Type=idle

[Install]

WantedBy=basic.target

root@galileo:~#

I made it executable and enabled the service.

Edit: It didn't work because one indent wasn't right.

import time

x = 0

f = open('/home/root/test.txt','w')

while x < 10:

print x

x += 1

f.write(str(x)+"\n")

time.sleep(1)

Highlighted
Community Manager
18 Views

I have not had to call a shell script to launch a python process on startup. I am able to do it all in python, which seems like a cleaner solution. So here is what I do.

Lets say you have a script "door.py" that monitors a switch. You want it to start on boot up. The file is in /root.

Create a systemd unit file in /lib/systemd/system called doormon.service.

[Unit]

Description=Door Monitor Alet App

[Service]

PIDFile=/var/run/door.pid

WorkingDirectory=/home/root

ExecStart=/usr/bin/python door.py --pid

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s TERM $MAINPID

[Install]

WantedBy=basic.target

Now notice I am creating a pid file, this lets systemctl stop and reload work.

In your python script use os.getpid() to create a pid file.

fh=open("/var/run/door.pid", "w")

fh.write(str(os.getpid()))

fh.close()

Then use systemctl enable doormon.service

And start your process with systemctl start doormon

Some examples can be seen here:

http://www.instructables.com/id/Intel-Galileo-Garage-Monitor/ http://www.instructables.com/id/Intel-Galileo-Garage-Monitor/

and

https://github.com/joemcmanus/galileoGarageMon https://github.com/joemcmanus/galileoGarageMon

-Joe