When you first encounter Amazon Linux AMI, you might notice the /etc/systemd/system/
directory exists, leading you to believe systemd is available. However, trying to run systemctl
gives the frustrating "command not found" error. This happens because Amazon Linux AMI traditionally uses SysV init by default.
Before making any changes, verify your current initialization system:
stat /proc/1/exe | grep -o 'systemd\|init'
If it returns "init", you're using the traditional SysV init system.
Amazon Linux 2 makes this straightforward:
sudo amazon-linux-extras install -y systemd
For older Amazon Linux AMI versions, you'll need to:
sudo yum install -y systemd
After installation, configure your system to use systemd:
sudo systemctl daemon-reexec
sudo systemctl preset --preset-mode=enable --all
Then reboot your instance:
sudo reboot
After reboot, check if systemd is now running:
ps -p 1 -o comm=
This should return "systemd". You can now use all systemd commands normally.
Here's an example service unit file for a Node.js application at /etc/systemd/system/nodeapp.service
:
[Unit]
Description=Node.js Application
After=network.target
[Service]
ExecStart=/usr/bin/node /var/www/app/server.js
Restart=always
User=nodeuser
Group=nodeuser
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
With systemd enabled, you can:
sudo systemctl start nodeapp
sudo systemctl enable nodeapp
sudo systemctl status nodeapp
journalctl -u nodeapp -f
Remember that Amazon Linux 2 (not the original AMI) has better systemd support. Some older AMI instances might require additional configuration for certain systemd features. Also, ensure your startup scripts don't conflict with both init systems.
If you encounter issues:
# Check systemd logs
journalctl -xe
# Verify systemd version
systemd --version
# Check for failed units
systemctl --failed
When working with Amazon Linux AMIs, you might notice the presence of /etc/systemd/system/
directory but find that systemctl
commands don't work. This happens because Amazon Linux traditionally uses SysV init by default, even though it includes some systemd infrastructure.
First, verify which init system your instance is using:
stat /proc/1/exe | grep -o "systemd"
If this returns "systemd", you're already using it. If not, you'll need to enable it.
For Amazon Linux 2 (the current generation), follow these steps:
# Install systemd if not present
sudo yum install -y systemd sysvinit-tools
# Enable systemd as default
sudo systemctl daemon-reload
sudo systemctl enable systemd-sysv-install
# Reboot to activate
sudo reboot
Once systemd is active, you can create services. Here's a sample web server service:
[Unit]
Description=My Web Application
After=network.target
[Service]
Type=simple
User=ec2-user
WorkingDirectory=/home/ec2-user/app
ExecStart=/usr/bin/python3 -m http.server 8080
Restart=always
[Install]
WantedBy=multi-user.target
With systemd running, you can now use all standard commands:
# Start service
sudo systemctl start myapp.service
# Enable auto-start on boot
sudo systemctl enable myapp.service
# Check status
sudo systemctl status myapp.service
If you encounter "Failed to get D-Bus connection" errors, ensure your instance has:
# Required capabilities
CAP_SYS_ADMIN
CAP_SYS_BOOT
And that your security groups allow necessary communication.
Systemd provides better boot performance metrics. Compare with:
systemd-analyze
systemd-analyze blame