For part 2 of DevOps we were assigned to install a linux distribution, run a server, and use crontab to schedule execution.

The linux distribution that i chose to use is that of Ubuntu 18.10 because i already had it installed as a virtual machine.

29985a98-ubuntu-logo32

For setting up the server I installed nodejs with the following command:

sudo apt install nodejs

With nodejs installed, a script for running the server was created at a folder designated for testing. The script written was the following:

const http = require("http");
const hostname = "127.0.0.1";
const port = 8080;

const server = http.createServer((req, res) = >{
        res.statusCode = 200;
        res.setHeader("Content-Type", "text/plain");
        res.end("Server test\n");
});
server.listen(port, hostname, () = > {
        console.log("El servidor esta corriendo");
});

For testing the following bash command can be run:

node test_server.js

This will show us the following screen:

Untitled

After that it was just question of sheduling it to run. For now i set it for running 24/7, 365 for testing with the following code:

First to enter crontab the following command was run:

sudo crontab -e

Then the script used was the following

# 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                                                    
* * * * * /usr/bin/node /home/ferpart/Documents/Code/DevOps/test_server.js

Asterisks were used so that it runns all the time.

Advertisements