I have an embedded linux system that I need to run a python script whenever it boots. The python script needs to have a terminal interface so the user can interact and see outputs. The script also spawns another process to transfer large amounts of data over SPI, this was written in C.
I've managed to get the script to start on launch and have terminal access by adding
#reboot /usr/bin/screen -d -m python3 /scripts/my_script.py
to the crontab. I can then do "screen -r" and interact with the script. However if launched in this way the script fails to start the external SPI script. In python I launch the script with subprocess.Popen
proc=subprocess.Popen(["./spi_newpins,"-o","/media/SD/"+ latest_file"])
and this works perfectly whenever I manually launch the script, even within screen. Just not when it is launched by crontab. Does anyone have any ideas on how to get the spi subprocess to also work from crontab?
Fixed now, I had to add an absolute path to the spi_newpins function call
proc=subprocess.Popen(["/scripts/./spi_newpins","-o","/media/SD/"+ latest_file"])
Related
I need to setup a Jetson Nano device so that a Python script is launched everytime an Internet connection is available.
So, referring to this question, I did the following:
I created the 'run_when_connection_available' script:
#!/bin/sh
# create a dummy folder to check script execution
mkdir /home /user_name/dummy_folder_00
# kill previous instances of the system
pkill python3
# move to folder with python script and launch it
cd /home/user_name/projects/folder
/usr/bin/python3 launcher.py --arg01 --arg02 ...
# create another dummy folder to check script execution
mkdir /home /user_name/dummy_folder_01
I made this script executable and I copied it to /etc/network/if-up.d
Now, everytime I plug the ethernet cable out and in again, I can see the dummy folders are created in /home/user_name, but the python script isn't launched (at least, it doesn't appear in the system monitor). I tried running the command in the script from the terminal, and everything works fine, the python program starts as expected. Am I doing something wrong?
I'm trying to figure out something similar to you, but not quite the same..
This solution got my script python script running upon internet connection, I can check the logs and everything is working fine:
Raspbian - Running A Script After An Internet Connection Is Established
However, my script uses notify-send to send notifications to my window manager which I can't seem to get working with systemd - the script works when run inside of the user space so I assume it's something to do with systemd and Xorg. Hopefully that shouldn't be a problem for you, I hope this solves your issue.
You shouldn't need a bash script in the middle, I got systemd service to run my python script with chmod u+x <file>.py and putting #!/usr/bin/env python3 at the top of the python file so that it's executable directly under the .service file like so:
ExecStart=/path/to/file/file.py
Ok, I guess it was a matter of permissions, I solved it by running everything as user_name, so I modified the script as
sudo -user user_name /usr/bin/python3 launcher.py --arg01 --arg02 ...
Unfortunately, I can't seem to use Windows Task Scheduler as I don't have admin rights on my work computer, and when I sign in as admin to run tasks the Python script doesn't run. Because of this, the only alternative I've found is creating a bash script that will run my .py file and adding that to my programs that run on startup.
This seems to work fine whenever I restart my computer, but I notice that if I leave my computer on overnight the batch script will close and my Python script won't run. I've checked the log files for any errors and there don't seem to be any issues in the script, it just seems that the cmd prompt screen closes.
Here is what I put in my .bat file:
"C:\Python\Python37-32\python.exe" "T:\service.py" > T:\output.log 2>&1
cmd /k
Just trying to figure out- why is this happening? And is there any way I can just keep this script running as long as my computer is on?
You could attempt to create a Windows Service with PowerShell. Just have it automatically recover when it's terminated. See this as a reference.
https://github.com/yaplex/CodeSamples/blob/092159a847e330778494ea7b69ee84d120db7e36/PowerShell/Install-Windows-Service.ps1
Then you can monitor trends with Event Viewer as to why and when it's terminated.
Normally, I would use "blender -P script.py" to run a python script. In this case, a new blender process is started to execute the script. What I am trying to do now is to run a script using a blender process that is already running, instead of starting a new one.
I have not seen any source on this issue so far, which makes me concern about the actual feasibility of this approach.
Any help would be appreciated.
Blender isn't designed to be started from the cli and to then keep receiving more commands from the cli as it is running. It does however include a text editor that can open text files and run the text block as a python script, it also includes a python console that can be used to interactively type in commands while blender is running. You may also find this addon useful as it lets you to run a text block in the python console, this leaves you with an interactive session that contains the variables as they exist at the end of the scripts execution.
There is a cli option to run blender as a python console blender --python-console - the gui does not get updated while this console is running, so you could open and exec several scripts and then when you exit the console, blender will update it's gui and allow interactive use, or if you start in background mode -b then it will quit when you exit the console.
My solution was to launch Blender via console with a python script (blender --python script.py) that contains a while loop and creates a server socket to receive requests to process some specific code. The loop will prevent blender from opening the GUI, and the socket will handle the multiple requests inside the same blender process.
I have one main program "main.py" that may freeze occasionally. Whenever I detect this happens, I want to have a separate program "watch.py" get my Linux machine to restart. These scripts start at bootup automatically since I edited /etc/rc.local.
Right now /etc/rc.local looks like this -
python watch.py &
python main.py &
This should let both programs run simultaneously. When I notice main.py has frozen, I'll give a signal manually to watch.py (using a remote TCP connection) to restart. What should my python code or shell script be to actually restart the system when "watch.py" receives a signal?
Possible answers could be writing some python code to restart a Linux machine, writing some python code to exit with a certain argument, and upon noticing the argument, execute "sudo reboot".
I'm writing a program to monitor a python script running. There will be multiple instances of this script, which is a server, running from different locations on the computer. My program will be monitoring to see that all instances that are being "watched" are up and running and will restart them as necessary.
The server script cannot be edited.
My problem is that the server process just shows up as the python executable, and I am unable to determine the location of the specific server script on the computer.
Is there anyway to determine what script is actually running on a specific python/pythonw.exe process? And also its path?
ENV: Windows only
You can run the following command from the command line:
WMIC PROCESS get Caption,Commandline,Processid,ExecutablePath
This will show you which module is being executed.