Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Change default editor of crontab in linux from nano to vim:

export EDITOR=vim

You can put this code inside of ~/.bashrc.

NOTE: export is used to set an environment variable in linux.

#linux #crontab #editor #vim #nano
What is cronjob?
cron is a unix, solaris, Linux utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

What is crontab?
Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

What is a cron job
Cron job or cron schedule is a specific set of execution instructions specifing day, time and command to execute.

To see list current cronjobs use crontab -l.

Edit crontab file, or create one if it doesn’t already exist by issuing the command below:
crontab -e

It may get opened by nano by default. In case you want to change your default editor for crontab use the below command:
export EDITOR=vim

NOTE: if you want to persist this data you have to put the above export command inside of ~/.bashrc


The general form of a cronjob is like below:

* * * * *   command to be executed
In total we have 5 stars. From left to right, first star is the minute that you want to run your cronjob at (min (0 - 59)).

Second star is hour (0 - 23).

Third star is day of month (1 - 31).

Fourth star refers to month (1 - 12).

And last star which refers to day of week (0 - 6). Be careful that 0 is sunday!


Now let's create a sample cronjob that reset our eshop service at 22:00:00 everyday:

0 22 * * * svc -k /etc/service/eshop


The other stars say that run this script every day, every day of month and every month.

#linux #cron #cronjob #crontab
I sometimes forgot to pull data from git before start working on a project. To minimize the headache of merge conflict or having to reset head and stash your data you can set a cronjob to run every 10 minute or so. And inside of your bash script CD into eaach of your folders and issue git pull command.

If you have similar issues and you're solving it in a different manner, I'd be happy to hear about it. :)

#linux #mac #bash #script #git #cron #crontab #cronjob
If you forget to pull your projects from git in a regular interval and many users working on the same projects, then there is a solution for you!

Create a bash script file as follow and make it executable by chmod +x puller.sh:

puller.sh file content:

#!/bin/bash

echo 'Iterating over folders...'
for dir in *
do
test -d "$dir" && {
cd ${dir}
echo "git pull $dir"
git pull
cd ".."
} || {
echo "------> $dir is not a directory <-------"
}
done

NOTE: this file should reside in your folder's project root. In my case it is in /Your/Projects/Folder.

Now as a final step, put it in your crontab:

10 * * * * bash -c "cd /Your/Projects/Folder; bash puller.sh >> /var/log/git_pull_output.log"

#linux #git #pull #cronjob #crontab #cron #bash