How to run a command on Startup on Linux (works on CentOS & Ubuntu)

Hi there,

In this example I had a need to record boot up times in a file that i could access easily. To do so I decided to run a command on startup. This tutorial tool place on CentOS but the commands should also work on Ubuntu.

Step 1

Edit the rc.local file in vi by running:

sudo vi /etc/rc.local

Step 2

Add the following line to the file, update the home directory to represent your home directory.

  • Press i to go into insert mode
  • Enter the line:
    • echo “the computer booted up at” `date` >> /home/darren/bootup.log
  • Press ESC and then wq! and enter to save and quit the file.

Step 3

Give the executable  permission to the rc.local file by running the following command:

chmod +x /etc/rc.d/rc.local

Step 4

Reboot the system by running:

sudo init 6

Step 5

When the machine reboots run the following command to insure the file has been created and that there is content in the file:

cat /home/darren/bootup.log

A full video tutorial can be found here:

3 thoughts on “How to run a command on Startup on Linux (works on CentOS & Ubuntu)

  1. Ebtisam

    Hello Mr.Darren,

    Firstly, I would like to express my sincere gratitude forthe huge help you provided through your youtube channel.

    Secondly, I’m wondering of you can help me with my problem! I’m working on centos 7 and trying to install vue.
    I’ve used this link to help me with the commands:

    curl –silent –location https://rpm.nodesource.com/setup_8.x | sudo bash –
    yum install nodejs
    node -v
    npm -v
    npm install -g vue-cli (here i had some errors but the istallation completed)
    vue init webpack your-project (here i got this error:

    [eb@localhost ~]$ sudo vue init webpack /var/www/bsmdmn.com/public_html
    [sudo] password for eb:

    ? Target directory exists. Continue? Yes
    ? Project name itcproject
    ? Project description A Vue.js project
    ? Author Ebtisam
    ? Vue build standalone
    ? Install vue-router? Yes
    ? Use ESLint to lint your code? Yes
    ? Pick an ESLint preset Standard
    ? Set up unit tests Yes
    ? Pick a test runner jest
    ? Setup e2e tests with Nightwatch? Yes
    ? Should we run `npm install` for you after the project has been created? (recommended)
    npm

    vue-cli · Generated “/var/www/bsmdmn.com/public_html”.

    # Installing project dependencies …
    # ========================

    events.js:183
    throw er; // Unhandled ‘error’ event
    ^

    Error: spawn /bin/sh ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:362:16)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
    [eb@localhost ~]$ npm install @ionic/app-scripts@3.1.6 –save -dev
    npm WARN install Usage of the `–dev` option is deprecated. Use `–only=dev` instead.
    npm WARN deprecated browserslist@2.11.3: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.

    > node-sass@4.5.3 install /home/eb/node_modules/node-sass
    > node scripts/install.js

    Downloading binary from https://github.com/sass/node-sass/releases/download/v4.5.3/linux-x64-57_binding.node
    Download complete ] – :
    Binary saved to /home/eb/node_modules/node-sass/vendor/linux-x64-57/binding.node
    Caching binary to /home/eb/.npm/node-sass/4.5.3/linux-x64-57_binding.node

    > uglifyjs-webpack-plugin@0.4.6 postinstall /home/eb/node_modules/uglifyjs-webpack-plugin
    > node lib/post_install.js

    > node-sass@4.5.3 postinstall /home/eb/node_modules/node-sass
    > node scripts/build.js

    Binary found at /home/eb/node_modules/node-sass/vendor/linux-x64-57/binding.node
    Testing binary
    Binary is fine
    npm WARN saveError ENOENT: no such file or directory, open ‘/home/eb/package.json’
    npm WARN enoent ENOENT: no such file or directory, open ‘/home/eb/package.json’
    npm WARN eb No description
    npm WARN eb No repository field.
    npm WARN eb No README data
    npm WARN eb No license field.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {“os”:”darwin”,”arch”:”any”} (current: {“os”:”linux”,”arch”:”x64″})

    + @ionic/app-scripts@3.1.6
    added 650 packages from 611 contributors and audited 4988 packages in 164.218s
    found 0 vulnerabilities

    Would you please assist me?

    Thank you so much.

    Reply
  2. Pingback: How To Run A Command On StartUp in Linux (works on Centos & Ubuntu) – My blog

  3. Pingback: How To Run A Command On StartUp in Linux (works on Centos & Ubuntu) | All about hosting

Leave a Reply

Your email address will not be published. Required fields are marked *