Run a command during boot time

I wanted to add a command to crontab so that it is executed every time Linux boots so that I do not have to enter the command manually. I wanted to turn off the red light on my microphone outlet in Ubuntu.

NOTE: crontab files are maintained per user. There is one for root user and one for each of the other users.

To edit a crontab file for root –

ash@ash-MacBookPro:~$ sudo crontab -e
[sudo] password for ash:
no crontab for root - using an empty one

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]: 3
crontab: installing new crontab

The editor shows you this file –

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot amixer set IEC958 off
"/tmp/crontab.WLl8Df/crontab" 23L, 918C 1,1 All

For user ‘ash’ who is not root:

ash@ash-MacBookPro:~$ crontab -e
no crontab for ash - using an empty one
crontab: installing new crontab

There are various options available like reboot is for when a system reboots.
Similarly there are those for yearly, annually etc.

@reboot #Runs at boot
@yearly #Runs once a year [0 0 1 1 *]
@annually #Runs once a year [0 0 1 1 *]
@monthly #Runs once a month [0 0 1 * *]
@weekly #Runs once a week [0 0 * * 0]
@daily #Runs once a day [0 0 * * *]
@midnight #Runs once a day [0 0 * * *]
@hourly #Runs once an hour [0 * * * *]

You can get more information about crontab in the man page. You can list the contents of the crontab file using crontab -l command. For root, use ‘sudo’.

ash@ash-MacBookPro:~$ man crontab
ash@ash-MacBookPro:~$ crontab -l
no crontab for ash
ash@ash-MacBookPro:~$ sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot amixer set IEC958 off
ash@ash-MacBookPro:~$

In order to display crontab file for a normal user ‘ash’:

ash@ash-MacBookPro:~$ crontab -l

Have fun!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s