Sumit Kumar
Sumit Kumar

Reputation: 781

How to run linux commands using cron

I have a couple of linux commands I want to run everyday in the morning once using cron. Not sure how to do this though.

I understand that this needs to be done using shell, but I don't know how to do all this in linux. I will be able to create cron using cpanel though...

These are the commands

rm -rf <directory>
mkdir <directory>
chmod 777 <directory>

Upvotes: 2

Views: 17898

Answers (1)

Leandro Arruda
Leandro Arruda

Reputation: 506

You can create a shell script with this commands in a file script.sh, for example:

#!/usr/bin/bash

rm -rf <directory>
mkdir <directory>
chmod 777 <directory>

<others commands or logical instructions>...

In linux you can add a cron job in crontab, with crontab -e command, or in /etc/cron.d directory. The difference is that with command crontab -e the cron job will be set to user that execute crontab -e and add a cron job file right in cron.d you will need to put the user ahead the command of cron job.

Examples of cron jobs to be executed at 06:00 am.

With crontab -e:

0 6 * * * /usr/bin/bash /path_to_script/script.sh

Creating a file in /etc/cron.d:

0 6 * * * root /usr/bin/bash /path_to_script/script.sh

Alternatively you can just put the commands in your cron job as:

0 6 * * * rm -rf <directory> && mkdir <directory> && chmod 777 <directory>

Attention: remember to put the absolute path to directories that want to remove or create

PS: you can make your scripts in any language and use shell calls, as php with shell_exec() function or system() function of perl.

Upvotes: 2

Related Questions