Wednesday, October 30, 2013

Linux Crontab: Cron Job Examples


An experienced Linux sysadmin knows the importance of running the routine maintenance jobs in the background automatically.

Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.



Linux Crontab Format

MIN HOUR DOM MON DOW CMD
 
Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)
Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.

 

1. Scheduling a Job For a Specific Time

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.

30 08 10 06 * /home/ramesh/full-backup
  • 30 – 30th Minute
  • 08 – 08 AM
  • 10 – 10th Day
  • 06 – 6th Month (June)
  • * – Every day of the week

2. Schedule a Job For More Than One Instance (e.g. Twice a Day)

The following script take a incremental backup twice a day every day.

This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/ramesh/bin/incremental-backup
  • 00 – 0th Minute (Top of the hour)
  • 11,16 – 11 AM and 4 PM
  • * – Every day
  • * – Every month
  • * – Every day of the week

3. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours

This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/ramesh/bin/check-db-status
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • * – Every day of the week

Cron Job every weekday during working hours

This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

4. How to View Crontab Entries?

View Current Logged-In User’s Crontab entries

To view your crontab entries type crontab -l from your unix account as shown below.

ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

[Note: This displays crontab of the current logged in user]

View Root Crontab entries

Login as root user (su – root) and do crontab -l as shown below.

root@dev-db# crontab -l
no crontab for root

Crontab HowTo: View Other Linux User’s Crontabs entries

To view crontab entries of other Linux users, login to root and use -u {username} -l as shown below.

root@dev-db# crontab -u sathiya -l
@monthly /home/sathiya/monthly-backup
00 09-18 * * * /home/sathiya/check-db-status

5. How to Edit Crontab Entries?

Edit Current Logged-In User’s Crontab entries

To edit a crontab entries, use crontab -e as shown below. By default this will edit the current logged-in users crontab.

ramesh@dev-db$ crontab -e
@yearly /home/ramesh/centos/bin/annual-maintenance
*/10 * * * * /home/ramesh/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
 
When you save the above temporary file with :wq, it will save the crontab and display the following message indicating the crontab is successfully modified.
 
~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab

Edit Root Crontab entries

Login as root user (su – root) and do crontab -e as shown below.
 
root@dev-db# crontab -e

Edit Other Linux User’s Crontab File entries

To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.
 
root@dev-db# crontab -u sathiya -e
@monthly /home/sathiya/fedora/bin/monthly-backup
00 09-18 * * * /home/sathiya/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

6. Schedule a Job for Every Minute Using Cron.

Ideally you may not have a requirement to schedule a job every minute. But understanding this example will will help you understand the other examples mentioned below in this article.
 
* * * * * CMD
 
The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.
  • When you specify */5 in minute field means every 5 minutes.
  • When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute.
  • Thus the above convention can be used for all the other 4 fields.

7. Schedule a Background Cron Job For Every 10 Minutes.

Use the following, if you want to check the disk space every 10 minutes.
 
*/10 * * * * /home/ramesh/check-disk-space
 
It executes the specified command check-disk-space every 10 minutes through out the year. But you may have a requirement of executing the command only during office hours or vice versa. The above examples shows how to do those things.

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.
 
Table: Cron special keywords and its meaning
Keyword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.

8. Schedule a Job For First Minute of Every Year using @yearly

If you want a job to be executed on the first minute of every year, then you can use the @yearly cron keyword as shown below.

This will execute the system annual maintenance using annual-maintenance shell script at 00:00 on Jan 1st for every year.
 
@yearly /home/ramesh/red-hat/bin/annual-maintenance

9. Schedule a Cron Job Beginning of Every Month using @monthly

It is as similar as the @yearly as above. But executes the command monthly once using @monthly cron keyword.

This will execute the shell script tape-backup at 00:00 on 1st of every month.
 
@monthly /home/ramesh/suse/bin/tape-backup

10. Schedule a Background Job Every Day using @daily

Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell scriptat 00:00 on every day.
 
@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"

11. How to Execute a Linux Command After Every Reboot using @reboot?

Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.
 
@reboot CMD

12. How to Disable/Redirect the Crontab Mail Output using MAIL keyword?

By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below.
 
ramesh@dev-db$ crontab -l
MAIL="ramesh"

@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]

If you wanted the mail not to be sent to anywhere, i.e to stop the crontab output to be emailed, add or update the MAIL variable in the crontab as shown below.
 
MAIL=""

13. How to Execute a Linux Cron Jobs Every Second Using Crontab.

You cannot schedule a every-second cronjob. Because in cron the minimum unit you can specify is minute. In a typical scenario, there is no reason for most of us to run any job every second in the system.

14. Specify PATH Variable in the Crontab

All the above examples we specified absolute path of the Linux command or the shell-script that needs to be executed.

For example, instead of specifying /home/ramesh/tape-backup, if you want to just specify tape-backup, then add the path /home/ramesh to the PATH variable in the crontab as shown below.
 
ramesh@dev-db$ crontab -l

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh

@yearly annual-maintenance
*/10 * * * * check-disk-space

[Note: Crontab of the current logged in user with PATH variable]

15. Installing Crontab From a Cron File

Instead of directly editing the crontab file, you can also add all the entries to a cron-file first. Once you have all thoese entries in the file, you can upload or install them to the cron as shown below.
 
ramesh@dev-db$ crontab -l
no crontab for ramesh

$ cat cron-file.txt
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

ramesh@dev-db$ crontab cron-file.txt

ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

Note: This will install the cron-file.txt to your crontab, which will also remove your old cron entries. So, please be careful while uploading cron entries from a cron-file.txt.

Sunday, October 13, 2013

10 Useful Features Hidden in VLC

vlc-desktop-window

There’s a good chance you already use the VLC media player. But VLC isn’t just a media player — it’s a complete Swiss Army Knife for digital videos and music, filled with useful and fun features you haven’t found yet.

If you’re just using VLC to play back local media files, you’re only using a tiny fraction of VLC’s capabilities. There’s so much more you can do with VLC, whether you’re using it on Windows, Mac, or Linux.

Convert Media Files

RELATED ARTICLE
How to Shrink Videos to Fit Your Android Phone with VLC
If you want to put a video on your Android phone, you’ll probably need to shrink it down to fit... [Read Article]
VLC can convert media files between the formats it supports. You could use this to make a video smaller for a mobile device, convert media from an unsupported format to one your device supports, or even to extract the audio from a video and save it as a separate file.

To do this, click the Media menu and select Convert / Save. Load the file you want to convert, click the Convert / Save button, and select the type of file you want to convert it to. Use the Edit selected profile button to tweak the video encoding settings.

vlc-convert

Stream Media Over the Network or Internet

RELATED ARTICLE
How to Stream Videos and Music Over the Network Using VLC
VLC includes a fairly easy-to-use streaming feature that can stream music and videos over a local network or the Internet.... [Read Article]
VLC can stream media across the Internet or on your local network. To get started, click the Media menu, select Stream, provide the media file you want to stream and click the Stream button. You’ll be able to set up VLC as a media server so other computers on the network — or even around the world — can connect to your stream and view it.

Of course, if you want to stream over the Internet, you’ll probably need to forward ports on your router.

Record Your Desktop

RELATED ARTICLE
How to Record Your Desktop to a File or Stream It Over the Internet with VLC
VLC is full of powerful features, including the ability to record your desktop. VLC is great for quick captures, although... [Read Article]
VLC can load your desktop as an input device. This means that you can use the Convert / Save feature to save a video of your desktop, effectively turning VLC into screen capture software. You could also use this in concert with the Stream feature to broadcast a live stream of your desktop across the network or Internet with no additional software required.

vlc screencast header

Remotely Control Playback From a Browser

RELATED ARTICLE
How to Activate VLC’s Web Interface, Control VLC From a Browser, & Use Any Smartphone as a Remote
VLC includes a web interface, which you can enable to access your VLC player from a web browser, controlling playback... [Read Article]
VLC has an integrated HTTP server you can enable. Set this up and you can then remotely access your VLC client via a web browser. This would allow you to remotely control a media center PC from a web browser, controlling playback and queuing up audio or video files. You could even use this along with a smartphone to turn your phone into a remote control for VLC. There are mobile apps that function as remote controls for VLC, and these apps use VLC’s web interface to function.

image

Watch YouTube Videos

RELATED ARTICLE
How to Watch YouTube Videos in VLC Media Player
VLC Media player is capable of playing just about anything you can throw at it, but did you know that... [Read Article]
Want to play a YouTube video outside of your web browser? Just browse to a video on YouTube and copy its full URL — this should look something like the following:
https://www.youtube.com/watch?v=###########
Click the Media menu in VLC, select Open Network Stream, and paste the YouTube video’s URL into the box. VLC will load the video from YouTube and play it in a VLC window on your desktop.



When the video is playing, you could click the Tools menu and select Codec Information. You’ll see the full web address of the MP4 video displayed in the Location box, so you can copy-and-paste into a download manager — or just your web browser — to download the YouTube video to your computer.

Subscribe to Podcasts

VLC can be used to stream podcasts, so you don’t need any additional software if you want to listen to podcasts at your PC. Just click the View menu in VLC and select Playlist. Hover over Podcasts in the sidebar, click the + button, and paste the address of a podcast’s feed into the box. You can then stream the podcast’s episodes from within VLC.

vlc-subscribe-and-listen-to-podcast

Play Internet Radio

Before Pandora and Spotify, there were streaming Internet radio stations. There was a time when Internet users primarily streamed these radio stations from within Winamp, but they live on. You can view a searchable directory of radio stations from within VLC — just open the playlist and select Icecast Radio Directory. Perform a search for the kind of music you want to listen to or browse the list of free streaming radio stations.

Of course, VLC can also stream other Internet radio stations that aren’t included in this directory. You can generally find a “listen” link on their websites that will allow you to listen in a desktop player like VLC.

vlc-internet-radio-directory

Apply Video and Audio Effects

RELATED ARTICLE
Rotate a Video 90 degrees with VLC or Windows Live Movie Maker
Have you ever captured video with your cell phone or camcorder only to discover when you play it back on your computer that the video is rotated 90 degrees? Or maybe you shot it that way on purpose because you preferred portrait style to a landscape view? Before you go straining your neck or flipping your monitor on it’s side to watch your video, we’ll show you a few easier methods. [Read Article]
VLC can apply audio effects, video effects, and tweak the way a video’s audio and video line up. Click the Tools menu and select Effects and Filters. From here, you can apply an audio equalizer or video effects, such as cropping, rotating, overlaying, or colorizing a video. From the Synchronization tab, you can tweak the way a video’s audio and video stream line up. This allows you to fix broken videos where the audio and video are out of sync.

vlc-effects

As with other VLC features, these effects can be combined with other features. For example, you could permanently apply effects to a video by enabling these effects before using the Convert / Save feature.

ASCII Playback

RELATED ARTICLE
Stupid Geek Tricks: Watch Movies in Your Linux Terminal Window
In these days of high definition videos everywhere (even YouTube), only the truly geeky would decide to watch their movies in ASCII text in a terminal window. The surprising thing is that some videos are even fairly watchable. [Read Article]
ASCII playback isn’t a very useful feature, but it’s certainly an amusing one. In ASCII playback mode, VLC will display a video as ASCII characters rather than play it normally. It’s extremely impractical, but extremely geeky and worth a test drive if you want to surprise and amuse someone.

Click the Tools option in VLC, select Preferences, and click the Video icon. Click the Output box and select Color ASCII art video output. Save your settings, restart VLC, and start playing a new video. This feature works best with simple videos, such as cartoons with large sections of flat color.

vlc-ascii-art

After you’re done, go back into this window, click the Output box, and select Automatic to make VLC play videos normally.

Use a Video Wallpaper

RELATED ARTICLE
Set a Video as Your Desktop Wallpaper with VLC
Are you tired of static desktop wallpapers and want something a bit more entertaining? Today we’ll take a look at setting a video as wallpaper in VLC media player. [Read Article]
VLC also allows you to set a video as your desktop wallpaper, replacing your desktop background with a video. It’s not very practical and extremely distracting, but hey — it’s something you can’t do with many media players.

To do this, open VLC’s preferences window, click the Video icon, and select DirectX (DirectDraw) video output in the Output box. Restart VLC, load a video, and you’ll be able to click the Video menu and select Set as Wallpaper to turn the video into your desktop wallpaper.


When you’re done, just change VLC’s Output setting back to Automatic and restart it.


VLC is also available for other platforms, giving you excellent compatibility with different media formats on almost any device. In addition to Windows, Mac, and Linux, VLC can run on an Android phone or tablet or an iOS device like an iPhone, iPad, or iPod touch. VLC will also soon be available for Windows 8′s Modern interface and Windows Phone.

7 Skype Tips for Power Users

skype-login

Now that Skype has been merged with Windows Live Messenger, it’s more popular than ever. There’s no way to use Skype with a third-party client, but Skype does offer hidden features that can make it more powerful.
These are a few useful Skype tricks you won’t find unless you go off the beaten path, offering ways to sign into multiple Skype accounts, use IRC-style chat commands, record Skype calls, and even disable some of Skype’s built-in advertising.

Sign Into Multiple Skype Accounts

Unlike some messaging programs, Skype doesn’t allow you to easily log into multiple accounts. Try to launch the Skype shortcut once you’ve already opened Skype and it will just bring your already-open Skype window to the front. But you may have multiple Skype accounts — perhaps you have one for work and one for personal use.
Rather than open Skype as another Windows user account, Skype has a hidden option you can use to open a new Skype instance.
To do this, press Windows Key + R to open the Run dialog. In the Run dialog, enter the following command if you’re using a 32-bit version of Windows:
“C:\Program Files\Skype\Phone\Skype.exe” /secondary
On 64-bit versions of Windows, enter the following command instead:
“C:\Program Files (x86)\Skype\Phone\Skype.exe” /secondary
Skype will open a second Skype window, which you can log into as another Skype account. If you wanted to use this frequently, you could create a new Windows shortcut that opened Skype with the /secondary switch.

sign-into-multiple-skype-accounts[4]

Disable Contact List Advertisements

Skype will always show advertisements in its home pane, but it also shows advertisements at the bottom of your contact list by default. You can click the X button to close these advertisements whenever they appear, but they’ll just keep coming back. But there’s a better way — you can disable them permanently if you know where to look.

To disable the contact-list advertisements, or “promotions,” open Skype’s Options window, navigate to Notifications -> Alerts & messages, and uncheck the Promotions checkbox.

disable-skype-contact-list-promotions

Edit or Delete Sent Messages

This feature also isn’t immediately obvious if you’re used to other chat programs. If you make a mistake when typing a message or send a message you didn’t mean to, you can edit or delete a sent message later.
To do so, just right-click a message you’ve already sent and select Edit Message or Remove Message. When you edit a message, Skype will note that the message was edited — and if you delete a message, Skype will display “This message has been removed.”

Of course, if your recipient already saw the original message you sent, there’s no way to edit it out of their mind.

skype-edit-or-delete-message

Record Skype Calls

Skype doesn’t include a built-in call recording feature, but you may find yourself wanting to record a call at some point. Perhaps you’re interviewing someone remotely and you want to create a record of the interview that you can refer to later, perhaps you’re recording a podcast, or perhaps you’re having a business discussion and want a record of any agreements you make. There are plenty of good reasons you may want to record a call — aside from the obvious creepy ones.

As Skype doesn’t include a built-in call-recording feature, you’ll need to use a third-party application that will do the recording for you. There are several options out there — in the past, MP3 Skype Recorder was recommended to us by a reader and we found that it worked well.


Use Screen Sharing

Skype’s screen-sharing feature allows you to share your desktop with a Skype contact. You can use this feature to quickly troubleshoot someone’s PC without making them install additional remote access software. Or, you can us this feature to give a presentation and or show anything else remotely. You could also choose to share a single window instead of your entire desktop, giving yourself some privacy.

While on a Skype call, just click the + button and select Share screens. You could also just click the + button and select Share screens to initiate screen-sharing while not already on a call.

skype-share-screens
RELATED ARTICLE
The Best Tools to Easily Perform Remote Tech Support
“Help, my computer is broken!” comes the phone call yet again. If you’re stuck playing tech support for family or... [Read Article]
Bear in mind that, unlike traditional remote-access software, there’s no way to give someone else control over your screen. You’d have to walk the other person through any changes you make to their computer if you were acting as remote tech support via Skype screen-sharing.

Master Text-Based Chat Commands

If you’re a geek, there’s a good chance you’ve used IRC before. IRC offers a wide variety of chat features that are available as text-based commands, and Skype offers many comparable features.
In a Skype chat room, you can use the /add command to add a Skype user to the chat, use the /topic command to set a topic for that chat room, use the /setpassword command to set a password for that chat room, use the /setrole command to assign permissions to users in the chat, use the /kick command to kick a user from the chat, or use the /kickban command to kick a user and ban them from rejoining.

These are just a few of the chat commands Skype offers — check the What are chat commands and roles? page on the Skype website for a comprehensive list. You could also just use the /help command from within Skype to access a list of chat commands, although you’ll only see the full list if you’re in a chat room with more than two people.

skype-chat-commands

Easily Send a File to Multiple People

While you’re in a Skype chat with multiple people, you can easily send a file to them all by dragging and dropping the file into the chat room or conference call. Skype will give a copy of the file to everyone, allowing you to quickly share files without the hassle of sending them as an email attachment, sharing them via Dropbox, or even using Skype’s Send File feature to send it to them one at a time.
You could also create a group in your contacts, and then right-click the group and select Send File to send a file to every contact in that group at once. It’s an easy way to distribute files to several people at once.

skype-send-file

Skype may have become popular because it offered dead-simple voice and video calls over the Internet that “Just Worked” without any firewall configuration, but it’s more than a basic program. It offers quite a few power-user features that geeks will appreciate.