Linux Package, Process, Systemd, Cron

লেসন ১০ / ৩৮

📦 APT (Ubuntu)

sudo apt update                # always first!
sudo apt upgrade -y
sudo apt install nginx mysql-server php8.2
sudo apt remove nginx
sudo apt purge nginx           # + config
sudo apt autoremove
apt search nginx
apt show nginx
sudo apt-mark hold nginx       # no update

📚 Add Repository

# Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs

# PPA
sudo add-apt-repository ppa:ondrej/php
sudo apt install php8.3

🔄 Process

ps aux | grep nginx
htop                   # 🔥 (apt install htop)
btop                   # prettier
pgrep nginx
lsof -i :80

kill 1234              # SIGTERM
kill -9 1234           # force
kill -HUP 1234         # reload
pkill -f "node app.js"
killall php-fpm

🌅 Background

./script.sh &
nohup ./script.sh > out.log 2>&1 &

# tmux (best)
tmux                   # new session
# Ctrl+B then D to detach
tmux ls
tmux attach -t 0

⚙️ Systemd

systemctl status nginx 🔥
systemctl start/stop/restart nginx
systemctl reload nginx     # no downtime
systemctl enable nginx     # auto-start boot
systemctl list-units --type=service

📝 journalctl

journalctl -u nginx -f         # live 🔥
journalctl -u nginx -n 100
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -p err
sudo journalctl --vacuum-time=7d

🛠️ Custom Service

# /etc/systemd/system/myapp.service
[Unit]
Description=My Node App
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production
Environment=PORT=3000
StandardOutput=journal

[Install]
WantedBy=multi-user.target

# Activate
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
journalctl -u myapp -f

⏰ Cron

crontab -e
crontab -l

# min hour day month weekday cmd
0 0 * * *  /backup.sh                 # daily 12am 🔥
0 0 * * 0  /weekly.sh                 # Sunday
*/5 * * * * /check.sh                 # every 5 min
0 0,12 * * * certbot renew --quiet

# Laravel scheduler
* * * * * cd /var/www/laravel && php artisan schedule:run >/dev/null 2>&1

# Logging
0 0 * * * /backup.sh >> /var/log/backup.log 2>&1

🎓 মূল কথা ⭐

Systemd = production standard। journalctl -u app -f = best debug। Cron দিয়ে server auto-pilot — backup, renewal, cleanup।