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 http://icompressjs.com Here is the bash script that I wrote to monitor my local node install [crayon-59edf119d3339067298073/]     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-59edf119d334e613303068/] put above line just under echo "Launching" Now the final step is to cron this our bash script. I named it as node_monitor.sh 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-59edf119d3355513437733/] Above lines says that every 5th minute run node_monitor.sh 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-59edf119d4498583563443/] 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 "abc.com" and see how many lines have this string present. [crayon-59edf119d44a6875368271/] [crayon-59edf119d44ac746458141/] [crayon-59edf119d44b1176367484/] As you can see that we now are searching text "abc.com" 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-59edf119d44b8182489393/] Second command (equivalent to second script) : [crayon-59edf119d44bd778820668/] 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 http://blog.cloudflare.com/cloudflare-tips-recommended-steps-for-new-use 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 https://cloudflare.tenderapp.com/kb/why-do-i-see-cloudflares-ips-in-my-server-logs-or-site-logs 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-59edf119d514b980417307/] 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-59edf119d5159817889349/] Make sure that you have not changed your working directory Now we will restart our Apache service with the following command [crayon-59edf119d515f584671205/] OR [crayon-59edf119d516e335336615/]  Final step would be to update Apache Configuration and is done with the following command [crayon-59edf119d5175734200979/]   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 http://jaspreetchahal.org/ubuntu-12-04-download-youtube-videos-using-youtube-dl/ You will also need to install ffmpeg. You can install it using the command below [geshi lang="bash" nums="1" target="_self" ] [crayon-59edf119d5b18513343071/]   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-59edf119d5b31573347382/] 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-59edf119d5b3e986708717/] 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-59edf119d5b48367076137/] 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-59edf119d64b2735639358/] This command will install youtube-dl downloader tool. Now try this command [geshi lang="bash" nums="1" target="_self" ] [crayon-59edf119d64be628138692/] 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-59edf119d64c5358291820/] -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-59edf119d64cc020058042/]   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-59edf119d6ce4256852030/] 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-59edf119d6cf4337224006/] 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-59edf119d6d00931177468/] While fixing a problem I generally leave this command running to get the up2date log data [crayon-59edf119d6d0a459311818/] mySQL Logs file with CPanel you can get hold of .err file here [crayon-59edf119d6d16246356200/] Configuration file can be located here [crayon-59edf119d6d1d311265351/]     Apache Configuration File location httpd.conf can be found here [crayon-59edf119d6d23158575747/] You can viit like [crayon-59edf119d6d28280912342/]   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 http://linux.die.net/man/1/rsync 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-59edf119d76b9411384223/] [crayon-59edf119d76c7922099606/] [crayon-59edf119d76cd194836109/] and put your desired port in as shown below [crayon-59edf119d76d2229888473/] You will need to allow your firewall to allow port 2224 for incoming All done then restart your sshd service as [crayon-59edf119d76d8919979636/] Now I wanted to rsync from my local server to my production VPS at port 2224 this is how I did it [crayon-59edf119d76de313120946/] 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-59edf119d76e3868609028/] 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-59edf119d7e13141042870/] and to extract the above package you use this   [crayon-59edf119d7e1e477055450/] -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 http://unixhelp.ed.ac.uk/CGI/man-cgi?tar 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-59edf119d7e25478838372/] 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-59edf119d7e2b746800876/] 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-59edf119d8652369538799/] [crayon-59edf119d865e148692492/] 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-59edf119d8665067179234/] -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-59edf119d8c3c802116707/]     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-59edf119d8c48545022519/] 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...]