Useful Crontab Commands

I always end up searching to double check how to run programs at specific times using the Cron tab, so here are my useful ones.

First here’s a key to how crontab works.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Here are some commands to edit the crontab, note knowledge of using vi is pretty important here 🙂

Edit your crontab file, or create one if it doesn’t already exist.
[sourcecode language=”sh”]# crontab -e[/sourcecode]

Display your crontab file.
[sourcecode language=”sh”]# crontab -l[/sourcecode]

Remove your crontab file.
[sourcecode language=”sh”]# crontab -r[/sourcecode]

Here are some practical examples.

Daily at 4am.

[sourcecode language=”sh”]

0 4 * * *  path_to_program_to_run

[/sourcecode]

Weekly at 4am on Monday.

[sourcecode language=”sh”]

0 4 * * 1  path_to_program_to_run

[/sourcecode]

Monthly at 4am on the 1st of the month.

[sourcecode language=”sh”]

0 4 1 * *  path_to_program_to_run

[/sourcecode]

Monthly at 4am on the 1st and 15th of the month.

[sourcecode language=”sh”]

0 4 1,15 * *  path_to_program_to_run

[/sourcecode]

Public (requires no auth) and Private Shares with Samba

I just found out how to do this and thought I better blog it straight away as I couldn’t believe how easy it was.

I always just ignored the fact that Samba always asked for a user name and password from a windows machine and just ensured users were added to Samba with the same credentials they use to log onto their windows machine.

This is pretty good practise anyway as it allows users to have a private share that they only see when they log onto Samba.

But its also good to have just a public share that doesn’t require background authentication and acts just like when you share a folder on windows.

So enough of me beating around the bush, the key is adding just one line to your ‘/etc/samba/smb.conf‘ file.

Just add this line to your [Global] section.

[sourcecode language=”sh”]

map to guest = bad user

[/sourcecode]

Then if you haven’t already ensure your public share is the following:

[sourcecode language=”sh”]

[Public Dump]
path = /public_dump
writeable = yes
browseable = yes
guest ok = yes

[/sourcecode]

Finally just restart Samba to make the changes take effect.

[sourcecode language=”sh”]

# service smb restart

[/sourcecode]

Thinking about it this makes perfect sense as windows always sends your user name and password in the background when you access a share, so when you try to access the Samba share without being in the Samba user table, Samba will map you as a guest user leaving you with just being able to view the public shares you have set up.

How to Install and Format a new Linux Hard Drive

Anything in Linux is awalys slightly more taxing but is not that hard once you know how.

So first up you need to know whether you are working with a IDE and PATA (formerly just ATA) drive or a SCSI, SATA and USB drive.

Linux refers to IDE and PATA drives with “hdx” and SCSI, SATA and USB drives with “sdx“. Usually, a number is also put at the end of “hdx” or “sdx” to denote different partitions on the same physical drive, but for the purpose of formatting, you only need to know which letter the drive you want to format is.

To see all the drives attached to your system use the following commands.

[sourcecode language=”sh”]ls /dev/hd*[/sourcecode]

or

[sourcecode language=”sh”]ls /dev/sd*[/sourcecode]

In my case I have installed a new SATA hard drive so I am working with ‘/dev/sdb

First we will use ‘fdisk‘ to erase any old partitions on the drive, if you have a fresh new hard drive you wont need to do this.

[sourcecode language=”sh”]fdisk /dev/sdb[/sourcecode]

Now we can type ‘p‘ to see a partition table of the drive. The first line of output from the “p” command will also tell you the size of the drive. This is a good way to double-check that you are working with the correct drive.

To delete any existing partitions, press “d” and then “Enter.” It will ask you which partition number you wish to delete. The number of the partition is the number that follows ‘sdb‘. You can always view the partition table again with the “p” command.

Type “n” and hit “Enter.” Then press “p” to create a primary partition. It asks you for a partition number; enter “1” Now you are asked which cylinder the partition should start at. The beginning of the drive is the default, so just hit “Enter.” Then, you are asked for the last cylinder. The end of the drive is the default, so you can just press “Enter” again.

Now you are back at fdisk’s command prompt. Use the “p” command to check the partition table. You should now see your new partition at the bottom of the output. In the example, it lists as “/dev/sdb1

You now need to set the filesystem type for your new partition with the “t” command. You are asked for the Hex code of the file system you wish to use. We will use the standard Linux ext2 file system, which is “83

Now just issue the “w” command to write your new partition table and exit ‘fdisk

Finally we need to create the file system on the drive. We use the ‘mkfs‘ command.

[sourcecode language=”sh”]mkfs -t ext2 /dev/sdb1[/sourcecode]

Its good practise to now run a file system check on the new drive, we do this with the following command.

[sourcecode language=”sh”]fsck -f -y /dev/sdb1[/sourcecode]

Last but not least we need to mount the drive at boot. We do this by adding an entry into the ‘/etc/fstab‘ file.

[sourcecode language=”sh”]vi /etc/fstab[/sourcecode]

In my case the I want to mount the drive at ‘/meat‘ so we enter the follwing line in the ‘fstab‘ file.

[sourcecode language=”sh”]/dev/sdb1          /meat          ext2          defaults          0 0[/sourcecode]

Thanks to http://www.ehow.com/how_1000631_hard-drive-linux.html

Editing files with vi Commands

If you administrate servers you will almost definitely need to use vi command from the terminal as a GUI is not always possible, and vi is generally quicker to use I find.

These are my useful commands, ‘esc’ = the escape button.

Open a file:
[sourcecode language=”sh”]
vi filename
[/sourcecode]

Edit the file:
[sourcecode language=”sh”]
esc i
[/sourcecode]

Close the file:
[sourcecode language=”sh”]
esc :q
[/sourcecode]

Close and save the file:
[sourcecode language=”sh”]
esc :wq
[/sourcecode]