Basic Shell script to automatically start NodeJS if for some reason it stopped

Hi Guys, Well this one is a really basic example of a shell script that I used before in one of my production NodeJS website called Here is the bash script that I wrote to monitor my local node install [crayon-6691297c0d879071760888/]     as you can see that I am running Node on port 8080 and I am using curl to send a request to my node server. If response is 200 that is good and it will print "All Working well" else it will try to start NodeJS server You can also plugin another line to send email to you when a server is launched again. Something like this [crayon-6691297c0d87e518581043/] put above line just under echo "Launching" Now the final step is to cron this our bash script. I named it as You will have to give your local user rights to execute this script, after doing that you are able to cron it as shown below [crayon-6691297c0d87f909385577/] Above lines says that every 5th minute run If the response is ok i.e. 200 then nothing will be done else there will be an attempt to start NodeJS server again You can improve this script with better error handling but for what I was trying to do this was sort of enough I hope this helps Cheers         … [Read more...]

Linux Count lines in file and Sum count

Hi Guys, To start with I am intermediate user of Linux and use RedHat at work and Ubuntu at home. I had this job where I have to search my apache server logs for some "text". Count the number of lines that have this "text" in them. Well I know basic shell scripting so I just used a bit of basic commands to get what I wanted. If you know other better solution please leave yours in comments. [crayon-6691297c0dc7b075921615/] So DIRPATH above holds path to my access logs directory and I am iterating over to get line count of files one by one and my COUNTTOTAL variable holds the overall count. Above example show to count all lines and summing the output. In the example below lets make a slight change and search for a string "" and see how many lines have this string present. [crayon-6691297c0dc7e271370116/] [crayon-6691297c0dc7f282716415/] [crayon-6691297c0dc80491129041/] As you can see that we now are searching text "" in a file and piping the output to wc command adding the output to our global variable COUNTTOTAL and printing it using echo command. you should change DIRPATH to suit you. You can use the same fundamentals to create a dynamic list of file e.g. count the number of lines for files that are younger than 30 day, and iterate over them. We have used the following command in our shell scripts and each of the one have link to their manual page grep wc cut As my mate David Overton pointed out that above can be achieved without a script so here are our shell commands to do exactly the same thing. I think I am just stuck with scripting mind frame :) First command (equivalent to first script): [crayon-6691297c0dc81855345890/] Second command (equivalent to second script) : [crayon-6691297c0dc82376078254/] I hope that this helps. If I made any mistake please let me know. Cheers     … [Read more...]

Cpanel: Install mod_cloudflare

Hello, Now that I've put my site on CloudFlare, hopefully you will see a performance boost and site load time should drop. Guys at Cloudflare directed me to read this document which I think was sort of a kick start on "How To with cloudflare?" You can read that document here This document helped me a lot in understanding the configurations and how to use CloudFlare control centre. You must be aware that CloudFlare act as a proxy to your server thus all the IP addresses hitting your server would be CloudFlare's IP addresses. Your access logs will start looking weird. You should also read these documents Now this post just tackle one specific problem i.e.     How to install mod_cloudflare under CPanel Follow these steps Use putty or similar tool to connect to your server using SSH. Goto your home directory or goto /usr/local/src We will download the source code for mod_cloudflare now with the following command [crayon-6691297c0df42680275766/] Above step will download file called mod_cloudflare.c  We will now use apxs (Apache Extension Tool) to install our mod_cloudflare with the following command [crayon-6691297c0df45852050199/] Make sure that you have not changed your working directory Now we will restart our Apache service with the following command [crayon-6691297c0df46902929566/] OR [crayon-6691297c0df47025177189/]  Final step would be to update Apache Configuration and is done with the following command [crayon-6691297c0df48242631833/]   Make sure that there is no error from any of the above step. If you are not a system administrator then you should avoid doing it. But if you are a a techie and are confident enough then go ahead and do it. I also have compiled these instruction from … [Read more...]

Ubuntu Download youtube videos as mp3

Hello, In my last post I highlighted how you can download youtube videos using a great tool called youtube-dl In this post we will be covering a few things. Downloading youtube videos and later converting them to mp3 Downloading videos directly as mp3 We will be making use of ffmpeg and youtube-dl for our case. Lets go Preperation Just so that you know that you will need to install youtube-dl first on your Ubuntu box. If you haven't read my previous article please read that from here You will also need to install ffmpeg. You can install it using the command below [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e080588213762/]   Downloading video from youtube and converting them to mp3 using ffmpeg Lets download a video from youtube now. Considering that you are in /home/user/videos directory and you have write permissions to this folder, run this command [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e082474172944/] Your video will now be stored under /home/user/videos as C-2MmguSO4w.mp4 You can also specify a file name when using youtube-dl as shown below [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e083866264695/] Depending on your download speed it can take a while to download this video. Just in case if you got error saying "Unable to download" then you would like to read my previous post. Now we have C-2MmguSO4w.mp4 to work with. Lets convert it to mp3 The command [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e084824620407/] The above command will convert our mp4 file to a mp3 -ab 192000 in above command means that our mp3 should be encoded at 192Kbps for great quality -vn means that do not include video -i means input file name To know more about ffmpeg command read more … [Read more...]

Ubuntu 12.04 Download Youtube videos using youtube-dl

Hello, Now that I have finally settled on Ubuntu as my Linux distro at home, there is heck of a stuff to learn. Well one of the things that I used to do on my previous Linux distro was to download Youtube videos and convert them to other formats using ffmpeg. This post is all about showing you how to get running using a cool script called youtube-dl Just so that you are aware youtube-dl tool can use ffmpeg, so gone are the old days. This tool has grown to much of my liking now. Lets first of all install youtube-dl     [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e24a264215539/] This command will install youtube-dl downloader tool. Now try this command [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e24c737869402/] Your video will start downloading right away and will be stored in current folder (if you have write permission to it) by name ckYwfP9-pLE.mp4 As you can notice that video file name is exactly what query string param v's value is. Hang on but I got a "Unable to download video" error Well this is because you are running old version of youtube-dl installed, I guess the repo source does not get modified with each change. There is a easy way to update youtube-dl as shown below [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e24d635896148/] -U switch will check and update youtube-dl Try to download the above video again. Ta! Da! and its downloading correctly now. If not check your permission on folder that you are trying this command from. Advanced options For more advanced option you can always read man page of this tool by using the command below [geshi lang="bash" nums="1" target="_self" ] [crayon-6691297c0e24e781189602/]   In my next article I will show you how to download a youtube video as mp3 and some more examples. I hope that this helps. If you have anything to add please leave your comments. I can compare your … [Read more...]

CPanel Apache error_log and access_log location and location for mySQL logs

Hi Guys, So we all know that CPanel makes our life easier in many ways. Guys like me who are not Linux power users but more of intermediate level users love CPanel. I do! Well there is a strange overlapping when we have full root access to our VPS or Dedicated server. CPanel does help with many things and some things we like to do traditionally. For me error_log and access_log are one of those that I love to watch from my lovely shell. Now to get to the location of your log file you can do few things. You can get your access log location from apache configuration for each domain, but let me give you pointers to where these files are located Apache access_log files Complete apache access log can be found here [crayon-6691297c0e365039488603/] You can use command like tail to print last few lines of access log. Apache Domain specific access_log file If your server handles multiple domain requests then most likely they all have their own individual access_log files these can be located here [crayon-6691297c0e367019713879/] Once again you can use tail command to check out last few lines in domain specific log file Apache error_log These log are more frequently visited than the access_log ones, one reason is that all your ERRORs and Exceptions that Apache came across are logged in this file. Thus we can find a problem and issue a fix. The location for error_log files is [crayon-6691297c0e368664330234/] While fixing a problem I generally leave this command running to get the up2date log data [crayon-6691297c0e369492120943/] mySQL Logs file with CPanel you can get hold of .err file here [crayon-6691297c0e36a616521051/] Configuration file can be located here [crayon-6691297c0e36b383413611/]     Apache Configuration File location httpd.conf can be found here [crayon-6691297c0e36c751502485/] You can viit like [crayon-6691297c0e36d125599099/]   I … [Read more...]

Rsync to non standard SSH port

Hi Guys, While I was configuring my new VPS box I realized that it may not be a good thing to keep port 22 as default for SSH connections. Thus I decided that it will be port 2224 for SSH communication. I love putty as all others, I am not a typical system admin but I love playing around with few things. CPanel is one of those things. One of the software that I use too often is rsync You can become expert user of rsync by just following this manual anyway this is not about explaining what rsync is, you are reading this post because you already know what rsync is. Now coming back to my original point, so I configure my VPS disabled my port 22 and enabled my port 2224 for SSH communications, If you don't know how I did this here is a tip for you [crayon-6691297c0e488629671644/] [crayon-6691297c0e48a420470852/] [crayon-6691297c0e48b822353825/] and put your desired port in as shown below [crayon-6691297c0e48c341942268/] You will need to allow your firewall to allow port 2224 for incoming All done then restart your sshd service as [crayon-6691297c0e48d871830804/] Now I wanted to rsync from my local server to my production VPS at port 2224 this is how I did it [crayon-6691297c0e48e002307475/] As you can see is that trick is to instruct rsync to connect to the non standard port that is done by this bit "ssh -p NEW_PORT_NUMBER" Above can be rewritten for port 2224 as [crayon-6691297c0e48f870291478/] You can add other flags if you wish, many a times we try to preserve permissions and owners at the target so that is one thing I always do else everything gets messed up. I hope this helps Cheers Advertisement       … [Read more...]

Linux How to Create and extract tar.gz and tar.bz2 archives

Hi Guys, I guess this is a quick tip. Question is How we can create a tar.gz and tar.bz2 in Linux. I use these commands so often that I thought I should share because it could be helpful information. I am not exactly a Linux Pro but more of an intermediate user so your input will be always welcomed. Ok first I started using zip command in Linux a while back and loved it. It was quick and easy and was supported on many OS's like Linux and windows or even Mac. Compression with zip isn't always great then comes tar Now if you just use tar to compress a file that doesn't mean that you are compressing that file or group of files. Its more like packaging a file or group of files and folders. So basic usage of tar is shown below [crayon-6691297c0e5b6781985020/] and to extract the above package you use this   [crayon-6691297c0e5b9347775796/] -c above means create -x means extract The size is pretty much the same when you tar your files because it is just creating a archive for you files. Now lets jump into file compression using tar tar has to be told what algorithm need to be used to  compress your files and directories. So to lets use GZip If you take a look at man page You will notice that flag -z can be used to create a GZipped file which Lets change the above tar command that creates of package to the once below to create a Compressed Archive [geshi lang="text" nums="1" target="_self" ] [crayon-6691297c0e5ba809602081/] Notice that we added -z and .gz in the file extension. I added .gz so that I am clear when extracting an archive what algo I will need to use to extract the above So lets extract it [geshi lang="text" nums="1" target="_self" ] [crayon-6691297c0e5bc810779813/] See that because now I am aware that its gzipped from the extension thus I added -z to my flags else it will fail to extract if different algo is used to try to extract this archive. For me … [Read more...]

Linux Count number of Files in Directory

Hi Guys, I thought this may help you in some way. I am only a intermediate user of Linux but like to share stuff that I think could be useful to others. One of those things is How to count number of files in a given directory Well in simplest form browse to your directory and type this [geshi lang="text" nums="1" target="_self" ] # ls   | wc -l [crayon-6691297c0e6ca150495289/] [crayon-6691297c0e6cc076553860/] What this does is that its piping the output of list command to word count command. so if ls  return N number of lines then wc -l will be N. Second way to do it is just executing ls -l command as shown below [geshi lang="text" nums="1" target="_self" ] [user@web folder]# ls -l total 1397304 Notice the total value that the number of lines there. Now in the above command symbolic links are counted as files and also subdirectories get returned. You can have subdiretories excluded as shown with another Linux command called find [geshi lang="text" nums="1" target="_self" ] [crayon-6691297c0e6cd694038304/] -type f means read files only. So no symlinks are returned.   I hope that you found this post useful Cheers,   Advertisement     … [Read more...]

Linux Writing multiple lines to a file from command line

Hi Guys, We learn something new everyday. I did too. I know I am a late learner but its better late than never.   I asked myself a question about how to write multiple lines using say echo to a file. I generally will open my vi editor and and write something there and then save my file. and when I am using echo I never really had that situation where I need to write multiple lines to a file. But things changed :) Normally I will do something like this   Above will write word hello to file named test.txt in your current directory [crayon-6691297c0e7be725605290/]     Let me show you how to write multiple lines Trick is when you start you echo command follow it with " (double quotes) and don't close them until you are finished with your sentense as shown below [crayon-6691297c0e7c1865292840/] Above will write "Sit lectus dis placerat non aenean ultrices. Tempor, amet nisi in arcu, amet turpis, cras mattis ultrices, enim, penatibus. "  to your test.txt file. I hope this helps Cheers     … [Read more...]