Crontab every 30 seconds

 

To run a cron every 30 seconds check following example:

* * * * * /path/to/your/script arg1 arg2
* * * * * (sleep 30; /path/to/your/script arg1 arg2)

In above example both commands will start at 0th second of every minute, but the second command will sleep for 30 seconds and then start.

Linux view all users crontab

 

In general we will use crontab – e or crontab -l to view crontab.

Both commands crontab -e and crontab -l will show only current users(where we logged in) crontab.

To view other user crontabs use following command:

$ crontab -u <userName> -l

To list guest user crontab use following command:

$ crontab -u guest -l

Before running above command you need to login into root.

In Ubuntu based operating systems we need to use sudo with above command.

$ sudo crontab -u guest -l

To view all users crontab user following command:

$ for i in $(grep -P ":[0-9]{4}:[0-9]{4}" /etc/passwd | cut -d: -f1)
do 
    crontab -u $i -l; 
done 

Default crontab file location

 

Crontab configuration files of all users are available in path “/var/spool/cron/crontabs/”

If we list directory “/var/spool/cron/crontabs”, we can see root file and some files with usernames.

There will a crontab file for each user.

root file is for root user, user1 file is for user1, user2 file is for user2 and etc….

Depending on our requirement we can update these files.

To list available command in crontab use following command:

$ crontab -l

If we run crontab -l command from root user we can list commands configured in root users crontab and when they are going to execute, similarly we can list commands configured in other users crontab by logging into corresponding account.

To edit crontab use following command:

$ crontab -e

If you want to edit root user crontab login into root user and run crontab -e from root user.

Ubuntu change crontab editor permanently

 

By mistake if an uncomfortable editor is set for crontab, we can change it with following command:

$ select-editor

The output of above command will asks you to select any editor from list as shown below:

Output:

Select an editor. To change later, run ‘select-editor’.
1. /bin/ed
2. /bin/nano <—- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny

Choose 1-4 [2]: 4

Since I am comfortable with vim chosen option 4, which will set my crontab editor as vim.tiny.

After choosing an option just press enter and open crontab, it should opened with the editor you have selected. In my case it opened with vim.tiny.

There is one more post in my blog which will set the crontab editor only for that session.

Here is the link to change crontab editor for current session only.

-Sany