I hope this isnt a duplicate question. Systemd is really hard to search for....
I have a systemd file that looks like
[Unit]
Description=My Daemon
[Service]
User=root
Type=simple
PIDFile=/var/run/app.pid
ExecStart=/usr/bin/python /opt/app/app.pyc
Restart=always
[Install]
WantedBy=multi-user.target
I want ExecStart to run /usr/bin/python /opt/app/app.pyc if it exists and run /usr/bin/python /opt/app/app.py if it doesnt.
The goal that on the deployed system there will not be a py file only a pyc but on dev systems we might only have a py file. How can I get this to work?
Make a small bash script which does what you want and then put that script on the ExecStart line.
#!/bin/bash
if [ -f /opt/app/app.pyc ];
then
exec /opt/app/app.pyc
else
exec /opt/app/app.py
fi
Related
This is basically the same service file that the celery docs tells you to use as a basic beginners file.
With the below configuration, journalctl -ex displays the error "Failed at step CHDIR spawning /bin/sh: No such file or directory".
/etc/systemd/system/celery.service
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=apache
Group=apache
#Environment=PATH=/opt/python39/lib:/home/ec2-user/DjangoProjects/myproj
#Environment=PATH=/home/ec2-user/DjangoProjects/myproj
EnvironmentFile=/etc/conf.d/celery
#WorkingDirectory=/opt/python39
WorkingDirectory=/home/ec2-usuer/DjangoProjects/myproj
ExecStart=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi start $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}"'
ExecReload=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi restart $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
Restart=always
[Install]
WantedBy=multi-user.target
/etc/conf.d/celery
# Name of nodes to start
# here we have a single node
#CELERYD_NODES="w1"
# or we could have three nodes:
CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
#CELERY_BIN="/home/ec2-user/.local/bin/celery"
CELERY_BIN="/opt/python39/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
CELERYD_CHDIR="/home/ec2-user/DjangoProjects/myproj"
# App instance to use
# comment out this line if you don't use an app
#CELERY_APP="myproj"
CELERY_APP="myproj.celery_tasks"
#CELERY_APP="myproj.celery_tasks:myapp"
# ^^ ??? confusion ??? ^^
# or fully qualified:
#CELERY_APP="proj.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
# you may wish to add these options for Celery Beat
CELERYBEAT_PID_FILE="/var/run/celery/beat.pid"
CELERYBEAT_LOG_FILE="/var/log/celery/beat.log"
export DJANGO_SETTINGS_MODULE="myproj.settings"
If I leave out the WorkingDirectory in the service file, it throws this error: "ModuleNotFoundError: No module named 'myproj'".
I've spent the last 2 days looking at different configurations and what not, and I haven't been able to get past one of these 2 errors. What am I missing?
I was able to solve it!
I found this link that was a tutorial... which said that WorkingDirectory and CELERYD_CHDIR are the same.
I also read something on SO that suggested using a virtual environment... so I did that, too :).
The updated files:
/etc/systemd/system/celery.service
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=apache
Group=apache
#Environment=PATH=/opt/python39/lib:/home/ec2-user/DjangoProjects/myproj
#Environment=PATH=/home/ec2-user/DjangoProjects/myproj
EnvironmentFile=/etc/conf.d/celery
#WorkingDirectory=/opt/python39
WorkingDirectory=/home/ec2-user/DjangoProjects/myproj
ExecStart=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi start $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}"'
ExecReload=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi restart $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
Restart=always
[Install]
WantedBy=multi-user.target
/etc/conf.d/celery
# Name of nodes to start
# here we have a single node
#CELERYD_NODES="w1"
# or we could have three nodes:
CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
#CELERY_BIN="/home/ec2-user/.local/bin/celery"
#CELERY_BIN="/opt/python39/bin/celery"
CELERY_BIN="/home/ec2-user/.virtualenvs/myproj_prod/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
CELERYD_CHDIR="/home/ec2-user/DjangoProjects/myproj"
# App instance to use
# comment out this line if you don't use an app
#CELERY_APP="myproj"
#CELERY_APP="myproj.celery_tasks"
CELERY_APP="myproj.celery_tasks"
#CELERY_APP="myproj.celery_tasks:myapp"
# ^^ ??? confusion ??? ^^
# or fully qualified:
#CELERY_APP="proj.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
# you may wish to add these options for Celery Beat
CELERYBEAT_PID_FILE="/var/run/celery/beat.pid"
CELERYBEAT_LOG_FILE="/var/log/celery/beat.log"
DJANGO_SETTINGS_MODULE="myproj.settings"
After I created /var/run/celery/ and /var/log/celery/ folders, I ran chmod and gave access to the user and group that would be running the service to those folders - apache.
I have a server machine that has RHL6 (Red Hat Linux 6) and is based on SysV initialization (does not have systemd package), and I want to make my prometheus node exporter collect metrics from this machine.
All I can find online is how to create a node exporter service with systemctl (systemd): basically you create a .service file under /etc/systemd/system and then write something like this in it:
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
And then you start the service, enable it at startup, etc with systemctl command like this
sudo systemctl start node_exporter
sudo systemctl status node_exporter
sudo systemctl enable node_exporter
But the problem is that I don't have systemd installed and I don't have the right to update the server machine system so I am trying to find a way how to write an init script for node exporter to be placed under /etc/rd.d/init.d in my case.
It seems that all scripts under init.d are shell scripts that declare many methods like start(), stop(), restart(), reload(), force_reload(), ...
So it's not as easy as writing the service based on systemd.
Anyone have an idea how to do that with SysV init ???
Thanks,
I managed to found a solution for my problem.
Here is how the script looks like:
#!/bin/bash
#
# chkconfig: 2345 90 12
# description: node-exporter server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service node-exporter
start() {
echo -n "Starting node-exporter service: "
/usr/sbin/node_exporter_service &
### Create the lock file ###
touch /var/lock/subsys/node-exporter
success $"node-exporter service startup"
echo
}
# Restart the service node-exporter
stop() {
echo -n "Shutting down node-exporter service: "
killproc node_exporter_service
### Now, delete the lock file ###
rm -f /var/lock/subsys/node-exporter
echo
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status node_exporter_service
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
We place the above script under /etc/init.d named "node-exporter" (without .sh) and we place the binary for the node exporter under /usr/sbin (with systemd we place binaries under /usr/local/bin).
You can download the binary file for node exporter from here https://github.com/prometheus/node_exporter/releases.
Then we add the script file to the list of services with command chkconfig --add node-exporter (to check if it already exists use command chkconfig --list node-exporter).
Enable the service with command chkconfig node-exporter on.
And then to start/stop/restart ... we use command /etc/init.d/node-exporter start/stop/restart ....
In the start script we basically run the binary file and in the stop script we kill the process by its name.
I hope this will be useful.
I am working with systemd services in order to start an application. Stdout should be redirected to a file containing the current date (when the service was started). Logging to a file works fine, however, I don't know how to provide the date for the filename within the service. Any ideas?
...
[Service]
ExecStart=/bin/mybin
StandardOutput=file:/my/path/<filename should contain date>.log
...
systemd cannot generate the file name dynamically. But you can use bash redirection and date to create such a logfile.
[Service]
ExecStart=/bin/bash -c "/bin/mybin >/my/path/filename-$(date %%y-%%d-%%m).log"
I have the same needs and I have found a nice solution for me. It works well. Hope it can help you too.
1. Create an script.
You must put the script under /usr/bin. It's /usr/bin/ruoyi-gen.sh for me.
2. Add below contents.
#!/bin/bash
java -jar /root/xf-service/ruoyi-modules-gen-2.3.0.jar > /root/xf-service/ilogs/modules-gen-`date "+%Y-%m-%d"`.log 2>&1 &
Make the script executable -> chmod +x /usr/bin/ruoyi-gen.sh.
3. Add service description
Run vi /etc/systemd/system/ruoyi-gen.service, Add desciption like below:
[Unit]
Description=ruoyi-gen
[Service]
Type=forking
ExecStart=/usr/bin/ruoyi-gen.sh
[Install]
WantedBy=multi-user.target
4. Reload all systemd service files
systemctl daemon-reload
5.Start your service
systemctl start ruoyi-gen
It works on CentOS 7.
I would like to install postgresql in a linux (ubuntu) server.
If I do the following:
sudo apt-get install postgresql
it is going to install it in
/var/lib/postgresql/9.5/main
while I would like to have it in
/home/database/postgresql/9.5/main
Postgres uses the PGDATA environment variable to understand where do you want it to work. Edit the init script (either /etc/init.d/postgresql or /usr/lib/systemd/postgresql.service) and set the PGDATA accordingly. For example:
# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.
# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
[Unit]
Description=PostgreSQL database server
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.
# Location of database directory
Environment=PGDATA=/var/sql/pgsql/
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=/usr/local/pgsql/bin/postgresql95-check-db-dir ${PGDATA}
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
You may also try to symlink /var/lib/postgresql/9.5/main to /home/database/postgresql/9.5/main - might be simpler.
I'm trying to create a service / script to automatically start and controll my nodejs server, but it doesnt seem to work at all.
First of all, I used this source as main reference http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/
After testing around, I minimzed the content of the actual file to avoid any kind of error, resulting in this (the bare minimum, but it doesnt work)
description "server"
author "blah"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/var/www"
exec nodejs /var/www/server/server.js >> /var/log/node.log 2>&1
end script
The file is saved in /etc/init/server.conf
when trying to start the script (as root, or normal user), I get:
root#iof304:/etc/init# start server
start: Job failed to start
Then, I tried to check my syntax with init-checkconf, resulting in:
$ init-checkconf /etc/init/server.conf
File /etc/init/server.conf: syntax ok
I tried different other things, like initctl reload-configuration with no result.
What can I do? How can I get this to work? It can't be that hard, right?
This is what our typical startup script looks like. As you can see we're running our node processes as user nodejs. We're also using the pre-start script to make sure all of the log file directories and .tmp directories are created with the right permissions.
#!upstart
description "grabagadget node.js server"
author "Jeffrey Van Alstine"
start on started mysql
stop on shutdown
respawn
script
export HOME="/home/nodejs"
exec start-stop-daemon --start --chuid nodejs --make-pidfile --pidfile /var/run/nodejs/grabagadget.pid --startas /usr/bin/node -- /var/nodejs/grabagadget/app.js --environment production >> /var/log/nodejs/grabagadget.log 2>&1
end script
pre-start script
mkdir -p /var/log/nodejs
chown nodejs:root /var/log/nodejs
mkdir -p /var/run/nodejs
mkdir -p /var/nodejs/grabagadget/.tmp
# Git likes to reset permissions on this file, but it really needs to be writable on server start
chown nodejs:root /var/nodejs/grabagadget/views/layout.ejs
chown -R nodejs:root /var/nodejs/grabagadget/.tmp
# Date format same as (new Date()).toISOString() for consistency
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/nodejs/grabagadget.log
end script
pre-stop script
rm /var/run/nodejs/grabagadget.pid
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/nodejs/grabgadget.log
end script
As of Ubuntu 15, upstart is no longer being used, see systemd.