Automatically translated.View original post

Giveaway timer cron job with crontab

Phoy Timed Cron job with Crontab (or full name Cron Table)

Finish reading, enter Crontab by yourself. No need to reconcile the tool. See this photo is enough!

.

Many people will use Cron Job as a program to set the time to automation, such as CI / CD. If you set the time to work, you need Crontab to tell when to run this.

⏰ by setting the time using Crontab, there are 5 fields used to set the time:

1. Minute: Min. The insertable value is 0 - 59.

2. Hours: hours. Enter 0 - 23.

3. Days of the month: This is a calendar date, for example, on this day 1, the insertable value will be 1 - 31.

4. Month: Month. Of course there are 12 months. Put 1 - 12.

5. Days of the week: Days of the week, such as Monday, 0-6, starting from Sunday, will be 0, Monday is 1 until Saturday is 6, and 7 will be Sunday in some systems.

.

Entering a time value. Besides putting a number directly.

Other syntaxes can also be inserted.

- *: Every minute, for example. If in the field, every minute means

-,: Use the separator for cases that need to put more than 1 value in the field, such as weekdays. Enter 1,2,3,4,5.

- -: You can also put a range, for example, on weekdays, 1-5.

- * / n: Every n times, n must be divisible by, for example, in the field of minutes, 60 must be divisible by hours, and n is divisible by 60 (for minutes) or 24 (for hours).

.

The use of the face will look like this.

'< minute > < hour > < day of month > < month > < day of week > < command to execute >'

.

⭐ basic example

- Every day at 6pm, '0 18 * * *'

- Every Monday at 6pm, '0 18 * * 1'.

- Day 3 at 6 p.m. of every month. '0 18 3 * *'

⭐ a little harder example.

- Every Saturday at 5 p.m., 50 minutes. '50 23 * * 6'

- Every 1st and 16th day of every month at half past three o'clock, '30 15 1,16 * *'

- Every weekday at 7: 30 7 * * 1,2,3,4,5 'or' 30 7 * * 1-5 '.

- Every 30 minutes of every working day '* / 30 * * * 1,2,3,4,5' or '* / 30 * * * 1-5' as well.

- Every 4 hours. Minute 0 '0 * / 4 * * *'

- Every 2 hours from 9: 30 to 18: 30 '30 9-18/2 * * *'

- First Minute of the New Year Every Year '0 0 1 1 *'

- Every hour from 10 a.m. to 6 p.m. on Saturday Sunday '0 10-18 * * 6-7'

- Every minute there's her '* * * * *'

.

I'm not sure what my friends are doing or what interesting examples I can tell you. We haven't used it for a long time.

# Lemon 8 Howtoo # programmer # Developer # Includes IT matters # IT should know

2025/8/8 Edited to

... Read moreเวลาค้นคำว่า “cron job คือ” หรือ “crontab” ส่วนใหญ่จะเจอคนงง 2 เรื่อง: cron คือโปรแกรมตั้งเวลาอะไรสักอย่างบน Linux/Unix และ crontab คือไฟล์/ตารางที่เราเขียน “กฎเวลา” ให้ cron ไปเรียกคำสั่งตามเวลาที่กำหนดนั่นเอง สิ่งที่ผมใช้เช็กทุกครั้งก่อนใส่จริง (กันพลาด) 1) Cron job ทำงานด้วยผู้ใช้ไหน: cron ของแต่ละ user แยกกัน คำสั่งที่รันได้/สิทธิ์เข้าถึงไฟล์อาจไม่เหมือนตอนเรารันในเทอร์มินัลปกติ 2) Cron มักไม่โหลด environment เหมือน shell ปกติ: PATH อาจไม่ครบ ทำให้รันคำสั่งไม่เจอ วิธีแก้ง่าย ๆ คือใช้ “พาธเต็ม” เช่น /usr/bin/python3 หรือกำหนด PATH ไว้บนบรรทัดบนสุดของ crontab 3) ใส่ timezone ให้ชัด: เครื่อง server อยู่คนละโซนเวลาได้ ถ้าเป็นงานสำคัญควรเช็กเวลาเครื่อง (date) หรือกำหนด TZ ในระบบ/บริการให้ตรง คำสั่งที่ใช้บ่อย (แบบทำเองได้ ไม่ง้อ tool) - เปิด/แก้ไข crontab ของ user ปัจจุบัน: crontab -e - ดูรายการที่ตั้งไว้: crontab -l - ลบทั้งหมด (ระวังมาก): crontab -r ทริคการเขียนคำสั่งท้ายสุดให้ชัวร์ - แนะนำให้ redirect log ไว้ดูย้อนหลัง เช่น */30 * * * 1-5 /path/to/script.sh >> /var/log/myjob.log 2>&1 (2>&1 คือรวม error ไปที่ไฟล์เดียวกัน) ตัวอย่างที่คนชอบถาม (แนว cron cheat sheet เพิ่มเติม) - ทุก 5 นาที: */5 * * * * <command> - ทุกวันตอนเที่ยงคืน: 0 0 * * * <command> - ทุกวันจันทร์-ศุกร์ เวลา 9:00: 0 9 * * 1-5 <command> - ทุกสิ้นเดือน (ทำไม่ได้ตรง ๆ ใน cron แบบ 100%): ผมมักใช้วิธีรันทุกวันแล้วเช็กในสคริปต์ว่าพรุ่งนี้ขึ้นเดือนใหม่หรือยัง หรือเลือกใช้ 28-31 แล้วเช็กซ้ำในสคริปต์เพื่อกันรันเกิน ข้อควรระวังเรื่อง “*/n” ที่เจอบ่อย - */30 ในช่องนาที = ทุก 30 นาที (00,30) - แต่ถ้าอยาก “เริ่มที่นาทีที่ 10 แล้วทุก ๆ 30 นาที” ต้องเขียน 10-59/30 (ได้ 10,40) สรุป: ถ้าจำ 5 ฟิลด์ + สัญลักษณ์ (* , - /) ได้ และระวังเรื่อง PATH/log/timezone เวลาใส่ crontab ก็จะมั่นใจขึ้นมากครับ