DEV Community

laidah
laidah

Posted on

Crontab

Today I stumbled upon a flaky test. It's been bothering our developers for some time already, but usually, it took about two runs to have it turn green. So no big deal for them.
But it is for me.

As soon as I've opened it, I realized that this test is about cron jobs. And I know almost nothing about cron.
No worries! 10 minutes of intense googling and I am ready to tackle it.

Cron (Command Run ON) - is a program in Unix-based systems that can run scripts and tasks at specific time and order.

For example, you download a lot of cat pictures, videos, and memes every day, and o-oh! Your disk space is running low, there is no place to store your work files.
Have no fear! You can schedule crone to delete pictures and videos from download folder once a week.
Prepare your bash script:

cd /users/user_name/Downloads
find . -type f \( -name \*jpg -o -name \*png -o -name \*gif -o -name \*mp4 \) -delete
Enter fullscreen mode Exit fullscreen mode

Save it to file , for ex clean_dowloads.sh

Now open your terminal and write:
crontab -e
This will open a text redactor where you can write your task:

# Every Sunday at 00:00 run bash script clean_downloads.sh
@weekly bash /home/www/clean_dowloads.sh
Enter fullscreen mode Exit fullscreen mode

Save and exit. Now you are all set up, no more worries about too many cat pics.
Cron can schedule jobs using different time parameters:

# at every minute
* * * * *  <command>

# at 02:00 on Friday in July
0 2 * 7 5  <command>

# every 15 minutes after 2:00
*/15 2 * * * <command>
Enter fullscreen mode Exit fullscreen mode

Use Crontab.gury for easy planning.

And I should go back to my flaky test.
┐( ̄∀ ̄)┌

Top comments (1)

Collapse
 
pramon18 profile image
pramon18

Simple and really helpful. Good article.