Bu yazıda, Linux sistemlerinde çalışan ve durdurulmuş servisleri listeleyen ve kullanıcıdan alınan girdilere göre bu servislerin başlatma türlerini ayarlayan bir Bash script'ini inceleyeceğiz. Script,
Kod: Tümünü seç
systemctl
Script'in Genel Yapısı
Script, aşağıdaki adımları takip eder:
1.Servislerin Listelenmesi:
Tüm servislerin listesi alınır.
2.Kullanıcıdan Girdi Alma:
Kullanıcıdan kontrol etmek istediği servis adını ve başlatma türünü alır.
3.Servis Durumunu Kontrol Etme:
Girilen servis adının geçerli olup olmadığını kontrol eder.
4.Başlatma Türünü Ayarlama:
Kullanıcının seçimine göre servisin başlatma türünü ayarlar.
Sonuç olarak ;
Bu script, Linux sistem yöneticileri için oldukça kullanışlı bir araçtır. Kullanıcı dostu bir arayüz sunarak, servislerin durumunu kontrol etme ve başlatma türlerini ayarlama işlemlerini kolaylaştırır. Script, systemctl komutunu kullanarak sistem servislerini yönetmek için temel bir örnek sunmaktadır.
Kodlanan betiğin geliştirilmesi ...
Script'in daha kullanıcı dostu hale getirmek için hata kontrol mekanizmaları eklenecek.
Kullanıcıdan alınan girdilerin doğruluğunu kontrol etmek için ek validasyonlar (geçerlilik kontrolleri) yapılacak.
Script, belirli bir servis için daha fazla bilgi sağlamak üzere genişletilecek.
KOD İÇERİĞİ (Sürüm 1)
Kod: Tümünü seç
#!/bin/bash
# Sistemdeki tüm servislerin listesi (hem çalışan hem de durdurulmuş servisler)
echo "Tüm servisler listeleniyor..."
services=$(systemctl list-units --type=service --all --no-pager --quiet)
# Servisleri bir diziye ayırma
IFS=$'\n' read -d '' -r -a serviceArray <<< "$services"
# Servisleri kullanıcıya gösterme
echo "Mevcut servisler:"
for service in "${serviceArray[@]}"; do
# Servis adı ve durumu ayıklama
serviceName=$(echo $service | awk '{print $1}')
serviceDisplayName=$(echo $service | awk '{print $1}' | sed 's/\.service//')
serviceStatus=$(echo $service | awk '{print $3}')
# Durumunu da ekleyerek gösterme
echo "$serviceDisplayName ($serviceStatus)"
done
# Kullanıcıdan görünen servis adını alma
read -p "Lütfen kontrol etmek istediğiniz servis görünen adını girin: " displayName
# Kullanıcı iptal ederse
if [[ -z "$displayName" ]]; then
echo "İşlem İptal Edildi."
exit 0
fi
# Servis adı teknik adıyla eşleştirme
foundService=false
for service in "${serviceArray[@]}"; do
serviceName=$(echo $service | awk '{print $1}')
serviceDisplayName=$(echo $service | awk '{print $1}' | sed 's/\.service//')
if [[ "$displayName" == "$serviceDisplayName" ]]; then
foundService=true
break
fi
done
# Eğer servis bulunamazsa
if [[ "$foundService" == false ]]; then
echo "Görünen ad bulunamadı: $displayName"
exit 1
fi
# Kullanıcıdan başlatma türünü seçmesini isteme
echo "Başlatma türünü seçin:"
echo "1 - Otomatik"
echo "2 - Elle"
echo "3 - Devre Dışı"
read -p "Seçiminiz: " startType
# Eğer kullanıcı iptal ederse
if [[ -z "$startType" ]]; then
echo "İşlem İptal Edildi."
exit 0
fi
# Başlatma türünü belirleme
case $startType in
1)
startTypeStr="enable"
startTypeMessage="otomatik"
;;
2)
startTypeStr="start"
startTypeMessage="elle"
;;
3)
startTypeStr="disable"
startTypeMessage="devre dışı"
;;
*)
echo "Geçersiz seçim."
exit 1
;;
esac
# Servis adı ile birlikte .service uzantısını eklemek
serviceNameWithExtension="${displayName}.service"
# Komutu çalıştırma
echo "Servis $displayName ($serviceNameWithExtension) başlatma türü $startTypeMessage olarak ayarlanıyor..."
if [[ "$startTypeStr" == "enable" || "$startTypeStr" == "disable" ]]; then
# .service uzantısını doğru şekilde ekliyoruz
sudo systemctl $startTypeStr $serviceNameWithExtension
else
sudo systemctl $startTypeStr $serviceNameWithExtension
fi
# Başlatma türünü Türkçeleştirme
echo "Servis $displayName ($serviceNameWithExtension) başlatma türü $startTypeMessage olarak ayarlandı."
echo "İşlem tamamlandı."
Kod: Tümünü seç
~$ cd ~/Masaüstü
~$ ls -gh
toplam 76K
-rw-rw-r-- 1 linuxmint 57K Mar 1 02:20 'Akçakoca İmsakiye 2025.ods'
-rwxrwxrwx 1 linuxmint 1,3K Mar 1 16:22 'Hdd Failure Bash.sh'
-rw-rw-r-- 1 linuxmint 964 Mar 1 17:02 klasik_disk_bozulma_yüzdesi_2.vbs
-rw-rw-r-- 1 linuxmint 1,2K Mar 1 17:00 klasik_disk_bozulma_yüzdesi.vbs
-rwxrwxrwx 1 linuxmint 2,8K Şub 27 03:53 service_control.sh
~$ sudo ./service_control.sh
[sudo] linuxmint için parola: *****************
Tüm servisler listeleniyor...
Mevcut servisler:
accounts-daemon (active)
acpid (active)
alsa-restore (active)
alsa-state (inactive)
anacron (inactive)
apache2 (active)
apparmor (active)
apt-daily-upgrade (inactive)
apt-daily (inactive)
● (not-found)
avahi-daemon (active)
binfmt-support (active)
blk-availability (active)
blueman-mechanism (inactive)
bluetooth (active)
● (not-found)
● (not-found)
console-setup (active)
cron (active)
cups-browsed (active)
cups (active)
dbus (active)
dm-event (inactive)
dmesg (inactive)
dns-clean (inactive)
dpkg-db-backup (inactive)
● (not-found)
e2scrub_all (inactive)
e2scrub_reap (inactive)
emergency (inactive)
● (not-found)
finalrd (active)
fstrim (inactive)
fwupd-refresh (inactive)
getty-static (inactive)
getty@tty1 (active)
getty@tty7 (inactive)
gpu-manager (inactive)
grub-common (inactive)
grub-initrd-fallback (inactive)
ifupdown-pre (active)
irqbalance (active)
● (not-found)
● (not-found)
● (not-found)
● (not-found)
kerneloops (active)
keyboard-setup (active)
kmod-static-nodes (active)
● (not-found)
lm-sensors (active)
log_boot_time (inactive)
logrotate (inactive)
● (not-found)
lvm2-lvmpolld (inactive)
lvm2-monitor (active)
lxdm (active)
man-db (inactive)
minetest-server (active)
mintsystem (inactive)
ModemManager (active)
modprobe@configfs (inactive)
modprobe@drm (inactive)
modprobe@efi_pstore (inactive)
modprobe@fuse (inactive)
motd-news (inactive)
mysql (active)
netplan-ovs-cleanup (inactive)
● (not-found)
networkd-dispatcher (active)
networking (active)
NetworkManager-wait-online (active)
NetworkManager (active)
● (not-found)
● (not-found)
● (loaded)
● (not-found)
nvidia-persistenced (active)
● (not-found)
openvpn (active)
● (not-found)
packagekit (active)
phpsessionclean (inactive)
plocate-updatedb (inactive)
plymouth-quit-wait (active)
plymouth-quit (inactive)
plymouth-read-write (active)
plymouth-start (active)
polkit (active)
qemu-kvm (active)
● (not-found)
rc-local (inactive)
rescue (inactive)
● (not-found)
● (not-found)
rsyslog (active)
rtkit-daemon (active)
secureboot-db (inactive)
setvtrgb (active)
smartmontools (active)
● (not-found)
smbd (active)
systemd-ask-password-console (inactive)
systemd-ask-password-plymouth (inactive)
systemd-ask-password-wall (inactive)
systemd-backlight@backlight:intel_backlight (active)
systemd-backlight@backlight:nvidia_0 (inactive)
systemd-binfmt (active)
systemd-boot-system-token (inactive)
systemd-fsck-root (inactive)
systemd-fsck@dev-disk-by\x2duuid-8476\x2dE3A3 (active)
systemd-fsckd (inactive)
● (not-found)
systemd-initctl (inactive)
systemd-journal-flush (active)
systemd-journald (active)
systemd-logind (active)
systemd-machine-id-commit (inactive)
systemd-modules-load (active)
systemd-networkd (inactive)
systemd-pstore (inactive)
systemd-quotacheck (inactive)
systemd-random-seed (active)
systemd-remount-fs (active)
systemd-resolved (active)
systemd-rfkill (inactive)
● (loaded)
systemd-sysusers (active)
systemd-timesyncd (active)
systemd-tmpfiles-clean (inactive)
systemd-tmpfiles-setup-dev (active)
systemd-tmpfiles-setup (active)
systemd-udev-settle (active)
systemd-udev-trigger (active)
systemd-udevd (active)
● (not-found)
systemd-update-utmp-runlevel (inactive)
systemd-update-utmp (active)
systemd-user-sessions (active)
● (not-found)
thermald (active)
● (not-found)
ua-reboot-cmds (inactive)
ua-timer (inactive)
● (not-found)
ubuntu-advantage (inactive)
ubuntu-system-adjustments (active)
udisks2 (active)
ufw (active)
update-notifier-download (inactive)
update-notifier-motd (inactive)
upower (active)
user-runtime-dir@1000 (active)
user@1000 (active)
uuidd (inactive)
whoopsie (inactive)
● (not-found)
wpa_supplicant (active)
zfs-import-cache (inactive)
zfs-load-module (active)
zfs-mount (active)
zfs-share (active)
zfs-volume-wait (active)
zfs-zed (inactive)
Lütfen kontrol etmek istediğiniz servis görünen adını girin: log_boot_time
Başlatma türünü seçin:
1 - Otomatik
2 - Elle
3 - Devre Dışı
Seçiminiz: 1
Servis log_boot_time (log_boot_time.service) başlatma türü otomatik olarak ayarlanıyor...
Servis log_boot_time (log_boot_time.service) başlatma türü otomatik olarak ayarlandı.
İşlem tamamlandı.
~$