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

Execute python script when powering off

Jump to solution

I managed to /message/381193# 381193 run a python script at startup. Is the same possible when powering off?

Tags (1)
1 Solution
idata
Community Manager
76 Views

Hello BrechtW,

 

 

I have never tried to do something like this, however I believe it's possible since I found a few possible answers for this question in:

 

 

http://unix.stackexchange.com/questions/48973/execute-a-command-before-shutdown http://unix.stackexchange.com/questions/48973/execute-a-command-before-shutdown

 

http://ubuntuforums.org/showthread.php?t=185261 http://ubuntuforums.org/showthread.php?t=185261

 

http://stackoverflow.com/questions/18891312/how-to-run-a-shell-script-before-shutdown-on-centos http://stackoverflow.com/questions/18891312/how-to-run-a-shell-script-before-shutdown-on-centos

 

http://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot http://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot

 

 

Nevertheless, I have another idea. You could create your own command that executes the script you would like to run and then shuts down the Edison. This is actually very simple, just create a file in /usr/bin with a structure similar to the following:

 

 

# !/usr/bin/python

 

from subprocess import call

 

import time

 

call(["RUN YOUR SCRIPT IN HERE"])

 

time.sleep(0.2)

 

call(["poweroff"])

 

 

As you can see the command code is written in Python, it has a system call so you can run the script you would lie to run before powering off and when it finishes it shuts down the board. Once you've written the command, give it executable right with chmod +x YOUR_COMMAND and then you should be able to run this command any time.

 

 

In case you don't know how a system call works on Python, you can check the following link to learn more about it:

 

 

https://docs.python.org/2/library/subprocess.html https://docs.python.org/2/library/subprocess.html

 

 

Peter.

View solution in original post

1 Reply
idata
Community Manager
77 Views

Hello BrechtW,

 

 

I have never tried to do something like this, however I believe it's possible since I found a few possible answers for this question in:

 

 

http://unix.stackexchange.com/questions/48973/execute-a-command-before-shutdown http://unix.stackexchange.com/questions/48973/execute-a-command-before-shutdown

 

http://ubuntuforums.org/showthread.php?t=185261 http://ubuntuforums.org/showthread.php?t=185261

 

http://stackoverflow.com/questions/18891312/how-to-run-a-shell-script-before-shutdown-on-centos http://stackoverflow.com/questions/18891312/how-to-run-a-shell-script-before-shutdown-on-centos

 

http://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot http://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot

 

 

Nevertheless, I have another idea. You could create your own command that executes the script you would like to run and then shuts down the Edison. This is actually very simple, just create a file in /usr/bin with a structure similar to the following:

 

 

# !/usr/bin/python

 

from subprocess import call

 

import time

 

call(["RUN YOUR SCRIPT IN HERE"])

 

time.sleep(0.2)

 

call(["poweroff"])

 

 

As you can see the command code is written in Python, it has a system call so you can run the script you would lie to run before powering off and when it finishes it shuts down the board. Once you've written the command, give it executable right with chmod +x YOUR_COMMAND and then you should be able to run this command any time.

 

 

In case you don't know how a system call works on Python, you can check the following link to learn more about it:

 

 

https://docs.python.org/2/library/subprocess.html https://docs.python.org/2/library/subprocess.html

 

 

Peter.

View solution in original post

Reply