New FAMILUG

The PyMiers

Showing posts with label crontab. Show all posts
Showing posts with label crontab. Show all posts

Sunday, 16 October 2016

[Backup] Ký tự % trong crontab

Một ngày mưa, viết crontab backup dữ liệu về máy, gặp ngay lỗi
unexpected EOF while looking for matching ``'
Tức tự dưng một ký tự kết thúc file (EOF) xuất hiện trong câu lệnh chứa đoạn
scp SOMEWHERE xxx-`date +%Y%m%d_%H%M%S`.tar.gz
Với Linux based OSes, ký tự % trong file crontab không có giá trị gì đặc biệt.

Theo doc Ubuntu manpage 5 crontab
The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
Khi gõ man 5 crontab trên Ubuntu 16.04, ta có:

The ``sixth'' field (the rest of the line) specifies the command to be run.  The entire command  portion of  the  line,  up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file.  Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the  shell's trailing "\".
Xem online tại: http://manpages.ubuntu.com/manpages/xenial/en/man1/crontab.1posix.html
Tài liệu online của Ubuntu manpage không giống xem từ lệnh man, nó dùng nội dung của file man cung cấp bởi systemd-cron

Trên OSX, ký tự % cũng được xem như dấu xuống dòng (newline).
Theo man của OpenBSD:

The command field (the rest of the line) is the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab. Percent signs (‘%’) in the command, unless escaped with a backslash (‘\’), will be changed into newline characters, and all data after the first ‘%’ will be sent to the command as standard input. If the command field begins with ‘-q’, execution will not be logged. Use whitespace to separate ‘-q’ from the command.

Thursday, 23 April 2015

ghi chú nhanh về cron

1. cron gửi notification (email)
cron gửi output (cả stdout và stderr) của file cronjob hay các câu lệnh trong crontab tới user đã được gán cho biến môi trường ``MAILTO`` của cron. Nếu câu lệnh fail nhưng không có output,
syslog sẽ ghi lại lần chạy fail này nhưng KHÔNG CÓ output nào được gửi tới user cả (thường là root)  vẫn có email gửi về với tên của script đã fail và return code.
Bản chất cron là một daemon đọc file cấu hình từ /etc/crontab và lên lịch để chạy các câu lệnh đó (cron.daily, cron.hourly ...) chỉ là các giá trị (thư mục chứa executable files) được config sẵn trong /etc/crontab

2. Không cần restart cron
mỗi khi thêm/thay đổi một script vào một trong các thư mục chứa crontab file (/etc/cron\..+), không cần restart cron deamon vì cron sẽ tự động quét các thư mục này mỗi phút.
 Thus  cron need not be restarted whenever a crontab file is modified.
3. Tên file cronjob 

Thursday, 17 April 2014

Vài chú ý khi viết file cron/crontab

1. nên dùng absolute path của các file executable, ví dụ:

thay vì viết

rm XYZ

hãy viết

/usr/bin/rm XYZ

để lấy absolute path, dùng lệnh:

$ which rm
/usr/bin/rm