systemd分为系统service和用户service
- Ubuntu系统service在
/etc/systemd/system/
目录下,root用户使用 - CentOS系统service 在
/usr/lib/systemd/system/
目录下,root用户使用 - 用户service在
~/.config/systemd/user/
目录下,普通用户使用
systemd命令
systemctl | grep docker.service
# 列出所有service
systemctl list-units --type=service
# 列出所有running 和 failed的服务
systemctl list-units --type service --state running,failed
#查看状态、路径等
systemctl status docker.service
用户service
If you are going to run this as a user service (or you are going to use capistrano-sidekiq)
customize and copy this to ~/.config/systemd/user
Then run:
- systemctl --user enable sidekiq
- systemctl --user {start,stop,restart} sidekiq
Also you might want to run:
- loginctl enable-linger username
So that the service is not killed when the user logs out.
系统service
If you are going to run this as a system service
Customize and copy this into /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu) or /etc/systemd/system/(Ubuntu).
Then run:
- systemctl enable sidekiq
- systemctl {start,stop,restart} sidekiq
创建自定义service
vim /etc/systemd/system/lita.service
[Unit]
Description=Lita instance
Requires=redis.service
After=redis.service
[Service]
Type=simple
User=root
WorkingDirectory=/root/lita-slack
ExecStart=/usr/local/rvm/bin/rvm default do lita start
ExecReload=/bin/kill -TSTP $MAINPID
StandardOutput=append:/root/lita-slack/log/lita.log
StandardError=append:/root/lita-slack/log/lita_error.log
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target
- 加载自定义service
systemctl daemon-reload
OR
sudo /bin/systemctl daemon-reload
- 启动service
systemctl start lita
systemctl restart lita
- 开机启动
sudo /bin/systemctl enable lita
- 查看状态
systemctl status lita.service
# or 查看报错
journalctl -xe
# or
Use `journalctl -u lita -rn 100` to 看报错的最后100行
- 一个puma systemd例子
[Unit]
Description=Puma HTTP Server for battle_fury (production)
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/battle_fury/current
ExecStart=/usr/local/rvm/bin/rvm default do bundle exec puma -C /root/battle_fury/shared/puma.rb
ExecReload=/bin/kill -TSTP $MAINPID
StandardOutput=append:/root/battle_fury/shared/log/puma_access.log
StandardError=append:/root/battle_fury/shared/log/puma_error.log
Restart=always
RestartSec=1
SyslogIdentifier=puma
[Install]
WantedBy=multi-user.target
- 一个sidekiq systemd例子
[Unit]
Description=sidekiq
# start us only once the network and logging subsystems are available,
# consider adding redis-server.service if Redis is local and systemd-managed.
After=syslog.target network.target redis-server.service
# See these pages for lots of options:
#
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
#
[Service]
#
# !!!! !!!! !!!!
#
# As of v6.0.6, Sidekiq automatically supports systemd's `Type=notify` and watchdog service
# monitoring. If you are using an earlier version of Sidekiq, change this to `Type=simple`
# and remove the `WatchdogSec` line.
#
# !!!! !!!! !!!!
#
Type=notify
# If your Sidekiq process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
WorkingDirectory=/home/ubuntu/blogs
# If you use rbenv:
ExecStart=/bin/bash -lc 'exec ~/.rbenv/shims/bundle exec sidekiq'
ExecReload=/usr/bin/kill -TSTP $MAINPID
# If you use the system's ruby:
# ExecStart=/usr/local/bin/bundle exec sidekiq -e production
# If you use root user and rvm is installed with root user in production
# ExecStart=/usr/local/rvm/bin/rvm default do bundle exec sidekiq -e production
# If you use rvm in production without gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5@gemset-name/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and ruby version/gemset is specified in .ruby-version,
# .ruby-gemsetor or .rvmrc file in the working directory
#ExecStart=/home/deploy/.rvm/bin/rvm in /opt/myapp/current do bundle exec sidekiq -e production
# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process
# Uncomment this if you are going to use this as a system service
# if using as a user service then leave commented out, or you will get an error trying to start the service
# !!! Change this to your deploy user account if you are using this as a system service !!!
# User=deploy
# Group=deploy
# UMask=0002
# Greatly reduce Ruby memory fragmentation and heap usage
# https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
Environment=MALLOC_ARENA_MAX=2
# if we crash, restart
Restart=on-failure
#Restart=always #可能报错 Start request repeated too quickly, 使用on-failure 或者 always加RestartSec
RestartSec=1
#StartLimitBurst=2
# Restart, but not more than once every 30s (for testing purposes)
#StartLimitInterval=30
# output goes to /var/log/syslog (Ubuntu) or /var/log/messages (CentOS)
# StandardOutput=syslog
# 上面配置 syslog 报错:Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether.
StandardOutput=journal # sudo journalctl -u sidekiq_dev -f or sudo journalctl -u sidekiq_dev -n 100
or
StandardOutput=append:/root/blogs/shared/log/sidekiq.log # 暂时还不知道append和file的区别
StandardOutput=file:/root/blogs/shared/log/sidekiq.log
StandardError=journal
# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target
- journalctl 查看日志
journalctl --since="2021-09-16 14:22:02"
journalctl --since "30 min ago"
journalctl --since yesterday
journalctl --since "2021-01-01" --until "2021-09-16 13:40"
journalctl --since 07:30 --until "2 hour ago"