I have a python script1 which i use as a library which i import to my main script.
before using it i checked that it was working fine, i noticed that when i run this script1 with "sudo" then it doesn't find one of library he use.
sudo python3 -W ignore detector.py -d datasets -c MLP predict
By searching a little, I found that "sudo" did not use the same environment as my user account, and that it was necessary to add the argument "-E", and it works well.
sudo -E python3 -W ignore detector.py -d datasets -c MLP predict
the script1 also works well when I run it without "sudo"
python3 -W ignore detector.py -d datasets -c MLP predict
Then i import my script1 into my main script "import script1" and call one of the functions from script1 but get the same error from script1.
the problem is that my main script is managed by systemctl with service with root user and i can't use "-E" in ExecStart:
[Unit]
Description= Uplink manager with IA supervison of LORAWAN data
Wants=network-online.target
After=network-online.target
[Service]
ExecStart= /usr/bin/python3 -u/opt/flask_server/uplink_server/uplink_manager.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=uplink_manager.service
Can you help me ? Thanks.
The library that was missing, was in fact present in the root environment, but the version of the library was not right.
Related
This is my systemd service file:
[Unit]
Description=Foo
[Service]
WorkingDirectory=/tmp/test
ExecStartPre=/usr/bin/yq -o json config.yaml > config.json
ExecStart=/usr/bin/foo run -c config.json
[Install]
WantedBy=multi-user.target
Everything working if I run ExecStartPre and ExecStart in the shell.
But got error when I run systemctl start foo.service:
Error: open >: no such file or directory
Seems the ">" operator doesn't work as shell in the systemd file.
ExecStartPre doesn't use the shell to execute the command, so you can't use shell operators like redirection.
You'll need to invoke the shell explicitly.
ExecStartPre=bash -c '/usr/bin/yq -o json config.yaml > config.json'
Try:
ExecStartPre=/usr/bin/yq -o json config.yaml | tee -a config.json
or:
ExecStartPre=/bin/bash -c '/usr/bin/yq -o json config.yaml > config.json'
ubuntu 18
I have created a custom service at /etc/systemd/system/mycustomservice.service
and enable it : sudo systemctl enable /etc/systemd/system/mycustomservice.service
but the service does not load at start up, the content is:
[Unit]
After=mysql.service
[Service]
ExecStart=/home/myuser/runupdate.sh
[Install]
WantedBy=default.target
I try to execute the file /home/myuser/runupdate.sh without any issue
the permission of
/home/myuser/runupdate.sh is -rwxr--r--
/etc/systemd/system/mycustomservice.service is -rw-rw-r--
Please advise, thank you!
Systemd will need to know how to execute the script and what shell to use, hence there are two options Add:
#!/bin/bash
or
#!/bin/sh
to the top line of the script depending on the shell you are using. Alternatively, you can use:
ExecStart=/bin/bash -c /home/myuser/runupdate.sh
Trying to start Greenplum on system startup. Please find systemd service file content below.
[Unit]
Description=Greenplum daemon
[Service]
EnvironmentFile=/etc/environment
EnvironmentFile=/etc/default/greenplum
User=gpadmin
Group=gpadmin
Type=simple
ExecStartPre=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh"
#ExecStartPre=/opt/greenplum-db-6-6.11.2/greenplum_path.sh
ExecStart=/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1
Restart=on-failure
RestartSec=5s
PrivateTmp=true
[Install]
WantedBy=multi-user.target
All required environment variables are loaded and required path is sourced still not able to start service. Getting "ImportError: No module named gppylib.mainUtils". After machine is started if I run start command(/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1) it is working fine. I am not able to understand what is going wrong. My current setup has two hosts(master and segment host). I have kept segment node up and trying on master. Any help is much appreciated.
Sourcing greenplum_path.sh in ExecStartPre won't work because the environment isn't preserved between ExecStartPre and ExecStart. I would try something like
ExecStart=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh; gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1.
I have written a simple test code in python to toggle two of the I/O pins on and off every few seconds. I would like to be able to run this code whenever the board powers on so that I don't need to bring a keyboard, mouse, and monitor everywhere I want to run the simple test. How do I do this on Mendel OS on a google coral?
On Mendel OS, your systemd service should look like this:
myservice.service:
[Unit]
Description=Example systemd service.
After=weston.target
[Service]
Environment=DISPLAY=:0
PAMName=login
Type=simple
User=mendel
WorkingDirectory=/home/mendel
ExecStart=/bin/bash /usr/bin/test_service.sh
Restart=always
[Install]
WantedBy=multi-user.target
Regarding how to create a service and how to deploy it, you can follow this article.
Change 'ExecStart' line with your python file that you want to get executed.
Using crontab has been working consistently for me, you may want to add a time.sleep in the beginning of your python file
edit crontab
crontab -e
select nano editor
add
#reboot sudo python3 <path_to_your_script>
I had same issue.
This might be useful for you.
https://askubuntu.com/questions/919054/how-do-i-run-a-single-command-at-startup-using-systemd
I was able to add new service into systemd, but the script didn't run properly, but perhaps this won't be your problem.
I copied the instruction from the Nam Vu's note in Gist. This is like the details of Nanoj's answer above.
This is an example of starting a systemd object detection service on boot on the Coral Dev Board.
create a file called "detects.service" with similar with the following contents:
[Unit]
Description=systemd object detection service
After=weston.target
[Service]
PAMName=login
Type=simple
User=mendel
WorkingDirectory=/home/mendel
Environment=DISPLAY=:0
ExecStart=/bin/bash /usr/bin/detect_service.sh
Restart=always
[Install]
WantedBy=multi-user.target
Copy the file to "/lib/systemd/system/detects.service"
$ sudo cp -i detects.service /lib/systemd/system
Create a file called "detect_service.sh" with similar to following content:
edgetpu_detect --model fullpath/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite --label fullpath/coco_labels.txt
or
python detect.py --model fullpath/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite --label fullpath/coco_labels.txt
Make it executable and copy it to "/usr/bin":
$ sudo chmod u+x detect_service.sh
$ sudo cp -i detect_service.sh /usr/bin
enable the service with the systemctl command:
$ sudo systemctl enable detects.service
This would be useful when your python code called the Google "gstreamer code" example. The gstreamer code not able to be executed with sudo command, so you may not able to use with "sudo crontab -e" method for example Danny Dasilva's answer above.
I'm trying to write an "initial" cloud-config file that does a bit of setup before my default Cloud-Config file replaces it and takes over. This is what it looks like, however whenever it runs the "clustersetup.service", it can't find the clustersetup.sh file that was supposed to save. Course if I run this from a terminal it works just fine. What am I doing wrong?
#cloud-config
coreos:
etcd:
addr: $private_ipv4:4001
peer-addr: $private_ipv4:7001
fleet:
public-ip: $private_ipv4
units:
- name: clustersetup.service
command: start
content: |
[Unit]
Description=Cluster Setup
[Service]
ExecStartPre=/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh
ExecStart=/usr/bin/bash ~/clustersetup.sh
ExecStop=/usr/bin/bash
Paths specified by systemd cannot be relative. Try this again specifying the full path /home/core/clustersetup.sh.
In my distribution (ubuntu), bash is in /bin. One thing you could do is:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh'
ExecStart=/bin/bash -c ~/clustersetup.sh'
I think you will get the proper expansion of the ~ when pushing it through the shell. However, ~ will be relative to the process id executing the script (I don't know for certain that is core). If you wanted to be sure, you could:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~core/clustersetup.sh'
ExecStart=/bin/bash -c ~core/clustersetup.sh'
I haven't tested this. I agree with #Brian in that the explicit path would be a better idea. In general it is best not to get a shell involved with execution.