I start pm2on booting my raspberry pi 3 with ONE of theses shell scripts (rc.local executed as root during boot)
As root user
code
startPm2Root() {
#delete to avoid all pm2 issues
rm -rf "$PM2_HOME"
pkill -f pm2
USER="root"
HOME="/root"
PM2_HOME="$HOME/.pm2"
export USER HOME PM2_HOME
pm2 startup systemd &
if [ -f /home/pi/app/newVersion.tar.gz ]; then
bash /home/pi/app/deploy.sh
else
pm2 kill && pm2 start /home/pi/app/processes.json &
fi
pm2 save
}
problem/log
+ USER=root
+ HOME=/root
+ PM2_HOME=/root/.pm2
+ export USER HOME PM2_HOME
+ [ -f /home/pi/app/newVersion.tar.gz ]
+ pm2 startup systemd
+ pm2 save
+ pm2 kill
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'chmod +x /etc/systemd/system/pm2-root.service',
'systemctl enable pm2-root',
'systemctl start pm2-root',
'systemctl daemon-reload',
'systemctl status pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
>>> Executing chmod +x /etc/systemd/system/pm2-root.service
[DONE]
>>> Executing systemctl enable pm2-root
[DONE]
>>> Executing systemctl start pm2-root
[PM2] PM2 Successfully daemonized
[PM2] Stopping PM2...
[PM2][WARN] No process found
[PM2] All processes have been stopped and deleted
[PM2] PM2 stopped
+ pm2 start /home/pi/app/processes.json
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[DONE]
>>> Executing systemctl daemon-reload
[DONE]
>>> Executing systemctl status pm2-root
● pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled)
Active: active (running) since Fri 2017-06-30 16:36:29 UTC; 199ms ago
Docs: https://pm2.keymetrics.io/
Main PID: 1034 (PM2 v2.5.0: God)
CGroup: /system.slice/pm2-root.service
└─1034 PM2 v2.5.0: God Daemon (/root/.pm2)
Jun 30 16:36:27 grassberry pm2[1021]: [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] PM2 Successfully daemonized
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] Resurrecting
Jun 30 16:36:29 grassberry pm2[1021]: [PM2] Restoring processes located in /root/.pm2/dump.pm2
Jun 30 16:36:29 grassberry pm2[1021]: [PM2][ERROR] No processes saved; DUMP file doesn't exist
Jun 30 16:36:29 grassberry pm2[1021]: ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
Jun 30 16:36:29 grassberry pm2[1021]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
Jun 30 16:36:29 grassberry pm2[1021]: └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
Jun 30 16:36:29 grassberry pm2[1021]: Use `pm2 show <id|name>` to get more details about an app
Jun 30 16:36:29 grassberry systemd[1]: Started PM2 process manager.
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
As pi
code
startPm2Pi() {
#delete to avoid all pm2 issues
rm -rf "$PM2_HOME"
pkill -f pm2
USER="pi"
HOME="/home/pi"
PM2_HOME="$HOME/.pm2"
export USER HOME PM2_HOME
pm2 startup systemd -u pi & #also tried it without -u
if [ -f /home/pi/app/newVersion.tar.gz ]; then
bash /home/pi/app/deploy.sh
else
pm2 kill && pm2 -u pi start /home/pi/app/processes.json &
fi
pm2 save
}
problem
Error: connect EACCES /home/pi/.pm2/rpc.sock
Also it looks like I got multiple processes running:
root#gb:/home/pi# ps -aux | grep pm2
root 946 1.8 3.4 87908 32172 ? Sl 16:36 0:01 node /usr/bin/pm2 start /home/pi/app/processes.json
root 947 2.1 3.3 87872 31500 ? Sl 16:36 0:01 node /usr/bin/pm2 save
root 990 1.6 2.9 81372 28096 ? Ssl 16:36 0:01 PM2 v2.5.0: God Daemon (/root/.pm2)
root 1034 1.5 3.0 81892 28396 ? Ssl 16:36 0:01 PM2 v2.5.0: God Daemon (/root/.pm2)
root 1040 1.5 2.9 81136 28160 ? Ssl 16:36 0:01 PM2 v2.5.0: God Daemon (/root/.pm2)
pi 1090 7.5 2.9 81520 28216 ? Ssl 16:37 0:01 PM2 v2.5.0: God Daemon (/home/pi/.pm2)
Looks stable so far, I removed the startup script, I think it is either the one (startup) or the other (start).
Removed all fragments from startup for pi
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 unstartup systemd -u pi --hp /home/pi
and root
sudo pm2 unstartup
And stripped the code to:
startPm2Root() {
#delete to avoid all pm2 issues
rm -rf "$PM2_HOME"
pkill -f pm2
USER="root"
HOME="/root"
PM2_HOME="$HOME/.pm2"
export USER HOME PM2_HOME
#pm2 startup systemd & <= removed this
pm2 kill && pm2 start /home/pi/app/processes.json &
#pm2 save <= removed this, launches a second instance somehow
}
Related
I want to start my app as service from systemd but the app is not starting.
My unit file appstart.service looks like this:
[Unit]
Description=Application start
[Service]
Type=simple
User=ec2-user
ExecStart=/usr/bin/bash /home/ec2-user/project/restartScript.sh
SyslogIdentifier=App_start
[Install]
WantedBy=multi-user.target
RestartScript.sh should start the java app:
#!/bin/bash
export SPRING_PROFILES_ACTIVE="tst,development"
cd /home/ec2-user/project
pkill java
/usr/bin/java -jar /home/ec2-user/project/app.jar >>/home/ec2-user/project/web.log 2>>/home/ec2-user/project/web-error.log &
I am starting the app as a service this way using User Data on AWS EC2 instance:
#!/bin/bash
mkdir /home/ec2-user/project
cd /home/ec2-user/project
sudo wget -P /home/ec2-user/project/ https://tst.s3.eu-west-1.amazonaws.com/app.jar
chown -R ec2-user:ec2-user /home/ec2-user/project
sudo wget -P /home/ec2-user/project/ https://tst.s3.eu-west-1.amazonaws.com/restartScript.sh
sudo chmod 755 /home/ec2-user/project/restartScript.sh
cd /etc/systemd/system/
sudo wget /etc/systemd/system/ https://tst.s3.eu-west-1.amazonaws.com/appstart.service
sudo su
systemctl daemon-reload
systemctl enable appstart.service
systemctl start appstart.service
exit
The output I am getting when I start the EC2 instance this way is:
$ systemctl status appstart.service
● appstart.service - Application start
Loaded: loaded (/etc/systemd/system/appstart.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2022-08-25 13:35:52 UTC; 4min 19s ago
Process: 7328 ExecStart=/usr/bin/bash /home/ec2-user/project/restartScript.sh (code=exited, status=0/SUCCESS)
Main PID: 7328 (code=exited, status=0/SUCCESS)
Aug 25 13:35:52 ip-x-x-x-x.tst.local systemd[1]: Started Application start.
When I try to do
systemctl start appstart.service
Nothing changes. The application is not working.
Any idea why is this happening?
OS on the machine:
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
Nothing seems to be wrong with this, the service is running and the application runs and finishes successfully. The Active state of the service becomes inactive (dead) in case of failure it should be Failed and Main PID is (code=exited, status=0/SUCCESS).
To verify that the service is running correct, write this line somewhere in RestartScript.sh:
echo "Test" > test.txt
After starting the service you will find the created file near RestartScript.sh file.
I managed to resolve the issue by changing the WantedBy section in appstart.service file to default.target.
I try to install vnc-server on my centos 7 server by following the steps below:
1) We install vnc-server
sudo yum install tigervnc-server
After, you’ve installed the program, login with the user you want to run the VNC program and issue the below command in terminal in order to configure a password for the VNC server.
su - your_user # If you want to configure VNC server to run under this user directly from CLI without switching users from GUI
$ vncpasswd
add a VNC service configuration file for your user via a daemon configuration file placed in systemd directory tree. In order to copy the VNC template file you need to run the following command with root privileges.
cp /lib/systemd/system/vncserver#.service /etc/systemd/system/vncserver#:1.service
On the next step edit the copied VNC template configuration file from /etc/systemd/system/ directory and replace the values to reflect your user as shown in the below
vi /etc/systemd/system/vncserver#\:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l my_user -c "/usr/bin/vncserver %i -geometry 1280x720"
PIDFile=/home/my_user/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
After you’ve made the proper changes to VNC service file, reload systemd system initialization program to pick up the new vnc configuration file and start the TigerVNC server.
systemctl daemon-reload
# systemctl start vncserver#:1
# systemctl status vncserver#:1
# systemctl enable vncserver#:1
Obtaining the following error
systemctl daemon-reload
[root#ns363691 ~]# systemctl start vncserver#:1
Job for vncserver#:1.service failed because a configured resource limit was exceeded. See "systemctl status vncserver#:1.service" and "journalctl -xe" for details.
[root#ns363691 ~]# systemctl status vncserver#:1
● vncserver#:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver#:1.service; disabled; vendor preset: disabled)
Active: failed (Result: resources) since mié 2019-11-13 02:09:07 CET; 14s ago
Process: 7605 ExecStart=/usr/sbin/runuser -l root -c /usr/bin/vncserver %i -geometry 1280x720 (code=exited, status=0/SUCCESS)
Process: 7593 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
nov 13 02:09:04 ns363691 systemd[1]: Starting Remote desktop service (VNC)...
nov 13 02:09:07 ns363691 systemd[1]: Can't open PID file /home/root/.vnc/ns363691:1.pid (yet?) after start: No such file or directory
nov 13 02:09:07 ns363691 systemd[1]: Failed to start Remote desktop service (VNC).
nov 13 02:09:07 ns363691 systemd[1]: Unit vncserver#:1.service entered failed state.
nov 13 02:09:07 ns363691 systemd[1]: vncserver#:1.service failed.
Any idea why the service does not start, what is this doing wrong? :(
I Wanted to run pygame script using systemd service for that
followed these steps to run a pygame script using systemd service
sudo systemctl daemon-reload
sudo systemctl enable service_name
sudo systemctl start service_name
and rebooted the system after that my-service don't want to run a pygame script for more understading
$ sudo journalctl -f -u rpi
-- Logs begin at Thu 2016-11-03 22:46:42 IST. --
Mar 28 12:19:11 raspberrypi systemd[1]: Started RPi-Service.
$sudo systemctl status rpi
rpi.service - RPi-Service
Loaded: loaded (/lib/systemd/system/rpi.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2019-03-28 12:19:14 IST; 22min ago
Process: 689 ExecStart=/home/pi/Documents/project1/allnewone (code=killed, signal=HUP)
Main PID: 689 (code=killed, signal=HUP)
My Service File
#rpi.service
[Unit]
Description= RPi-Service
After = multi-user.target
[Service]
Type = simple
ExecStart = /usr/bin/python3 /home/pi/Documents/project1/allnewone.py
Restart = on-abort
RestartSec = 5
KillMode = process
SendSIGHUP = no
[Install]
WantedBy=multi-user.target
Here is the solution
#rpi.service
[Unit]
Description= RPi-Service
After = multi-user.target
[Service]
Type = simple
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"
ExecStart = /usr/bin/python3 /home/pi/Documents/project1/allnewone.py
Restart = always
RestartSec = 5
KillMode = process
SendSIGHUP = no
[Install]
WantedBy= graphical.target
I am trying to start a service named pigpiod.service via systemd. It invokes a script with three commands. The second one is left out. Why is this?
/etc/systemd/system/pigpiod.service:
[Unit]
Description=Starts pigpiod
Before=touchscreen.service
[Service]
ExecStart=/home/sysop/pigpiod.sh
[Install]
WantedBy=multi-user.target
/home/sysop/pigpiod.sh:
#!/bin/sh
touch /home/sysop/before_pigpiod
/usr/bin/pigpiod
touch /home/sysop/after_pigpiod
When restarting the machine the two files get created in /home/sysop/, but pigpiod is not starting.
When starting the service manually via sudo systemctl start pigpiod the same happens.
When running sudo /home/sysop/pigpiod.sh manually pigpiod is actually starting!
This is the output of sudo systemctl status pigpiod -l right after boot:
● pigpiod.service - Starts pigpiod
Loaded: loaded (/etc/systemd/system/pigpiod.service; enabled)
Active: inactive (dead) since Sat 2017-09-16 20:02:03 UTC; 2min 29s ago
Process: 440 ExecStart=/home/sysop/pigpiod.sh (code=exited, status=0/SUCCESS)
Main PID: 440 (code=exited, status=0/SUCCESS)
Sep 16 20:02:02 kivypie systemd[1]: Starting Starts pigpiod...
Sep 16 20:02:02 kivypie systemd[1]: Started Starts pigpiod.
Why is it, that systemd skips the execution of /usr/bin/pigpiod, but manually running the script as root does not?
My system: Raspberry Pi Model 3B, Raspbian GNU/Linux 8 (jessie)
pigpiod forks without the -g option. So use Type = forking or use pigpiod -g
[Unit]
Description=Starts pigpiod
Before=touchscreen.service
[Service]
ExecStart=/home/sysop/pigpiod.sh
Type=forking
[Install]
WantedBy=multi-user.target
I'm trying to manage multiple redis instances with systemd. Below is my systemd unit file
[Unit]
Description=Redis instances
After=network.target
[Service]
Type=simple
ExecStart=/home/redis/bin/redis-server /home/redis/conf/redis-%i.conf
ExecStop=/home/redis/bin/redis-cli -p %i INFO # <= INFO should not stop redis process
User=redis
Group=redis
[Install]
WantedBy=multi-user.target
I could successfully start redis-server by
$ sudo systemctl start redis#6379
$ systemctl status redis#6379
● redis#6379.service - Redis instances
Loaded: loaded (/usr/lib/systemd/system/redis#.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2017-09-05 17:48:42 JST; 2h 52min ago
Main PID: 86962 (redis-server)
CGroup: /system.slice/system-redis.slice/redis#6379.service
└─86962 /home/redis/bin/redis-server 0.0.0.0:6379 [cluster]
As a test of stopping redis-server, I intentionally used INFO instead of SHUTDOWN at the ExecStop line.
But when I executed below command, systemd still killed my redis process.
sudo systemctl stop redis#6379
And there's no output by:
sudo journalctl -f -u redis#6379
I wonder how could this happen ?
PS:
I replaced redis-cli with an non-exist one:
ExecStop=/tmp/nonexist
journalctl showed error log like:
Sep 05 20:51:57 myhost systemd[96306]: Failed at step EXEC spawning /tmp/nonexist: No such file or directory
Sep 05 20:51:57 myhost systemd[1]: redis#6379.service: control process exited, code=exited status=203
Sep 05 20:51:57 myhost systemd[1]: Unit redis#6379.service entered failed state.
Sep 05 20:51:57 myhost systemd[1]: redis#6379.service failed.
But the running redis process was still killed.