ubuntu timer shutdown daily auto

ตอนตั้งค่าให้อ่านด้านล่าง  แต่ถ้ามีการแก้ไข .timer ให้ทำแบบนี้ด้วย

เมื่อคุณแก้ไขไฟล์ .timer หรือ .service แล้ว คุณจำเป็นต้องสั่งให้ systemd อ่านการตั้งค่าใหม่ครับ

ขั้นตอนที่ถูกต้องในการอัปเดตการตั้งค่าคือ:

https://chatgpt.com/share/68e645b3-3f3c-8012-a32e-e0604d720599

sudo nano /etc/systemd/system/daily-shutdown.timer   # แก้ไขเวลา


cat /etc/systemd/system/daily-shutdown.timer  # ดูเวลาที่แก้ไข


sudo systemctl daemon-reload                         # โหลดค่าใหม่

sudo systemctl restart daily-shutdown.timer          # รีสตาร์ท timer

systemctl list-timers --all | grep shutdown          # ตรวจสอบเวลาใหม่


 


For Ubuntu 24.04 Server, the best methods for scheduling an automatic shutdown without a user being logged in are systemd timers or cron jobs. These are the standard, reliable methods for running automated tasks on a Linux server.


1. Using systemd Timers (Recommended for 24.04)

systemd is the modern and preferred init system for Ubuntu. Using its built-in timer functionality is the most robust way to schedule tasks.

Step 1: Create a service file

This file tells systemd what command to run. Create a new service file named daily-shutdown.service in the /etc/systemd/system/ directory.

Bash
sudo nano /etc/systemd/system/daily-shutdown.service

Add the following content to the file:

Ini, TOML
[Unit]
Description=Daily server shutdown service

[Service]
Type=oneshot
ExecStart=/usr/sbin/shutdown -h now

Save and close the file.

Step 2: Create a timer file

This file defines when the service should run. Create a file named daily-shutdown.timer in the same directory.

Bash
sudo nano /etc/systemd/system/daily-shutdown.timer

Add the following content, adjusting the OnCalendar line to your desired shutdown time. For example, 03:00:00 means 3:00 AM.

Ini, TOML
[Unit]
Description=Timer to trigger daily server shutdown

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target

Save and close the file.

Step 3: Enable and start the timer

Finally, enable the timer so it starts automatically on boot and then start it manually for the first time.

Bash
sudo systemctl enable daily-shutdown.timer
sudo systemctl start daily-shutdown.timer

You can check the timer's status with sudo systemctl list-timers.


2. Using cron (Alternative)

cron is a classic and very reliable tool for scheduling tasks. It's simple and works on all Linux systems.

Step 1: Open the crontab editor for the root user

This ensures the job has the necessary permissions to shut down the system.

Bash
sudo crontab -e

Step 2: Add the shutdown entry

Add a new line at the end of the file. The syntax is minute hour day_of_month month day_of_week command.

  • To shut down every day at 3:00 AM:

    Code snippet
    0 3 * * * /usr/bin/shutdown -h now
    
  • To shut down every Friday at 10:00 PM:

    Code snippet
    0 22 * * 5 /usr/bin/shutdown -h now
    

Step 3: Save and exit

Save the file, and the cron job will be scheduled automatically.

Both methods will reliably shut down your Ubuntu 24.04 server at the specified time without requiring a user to be logged in. systemd is generally the more modern and integrated approach for current Ubuntu versions, but cron is just as effective for this task.

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Ehlib Delphi Selection contains a component introduced in an ancestor form Which can not be deleted

DMZ คืออะไร

ตั้งค่า restart server อัตโนมัติ โดยใช้ crontab