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-64832a520db86740299780/]     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-64832a520db8b147567404/] 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-64832a520db8d959624711/] 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-64832a520dfaf086613616/] 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-64832a520dfb2903743664/] [crayon-64832a520dfb3385306043/] [crayon-64832a520dfb4769755302/] 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-64832a520dfb5207213650/] Second command (equivalent to second script) : [crayon-64832a520dfb6990676176/] 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-64832a520e311324926378/] 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-64832a520e314741127283/] Make sure that you have not changed your working directory Now we will restart our Apache service with the following command [crayon-64832a520e315318043971/] OR [crayon-64832a520e316520982485/]  Final step would be to update Apache Configuration and is done with the following command [crayon-64832a520e317522362584/]   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-64832a520e438041616651/]   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-64832a520e43a731628413/] 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-64832a520e43b235229478/] 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-64832a520e43c209155179/] 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-64832a520e680366979149/] This command will install youtube-dl downloader tool. Now try this command [geshi lang="bash" nums="1" target="_self" ] [crayon-64832a520e683640230476/] 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-64832a520e684632188298/] -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-64832a520e685336840814/]   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-64832a520e77f278707680/] 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-64832a520e781404998524/] 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-64832a520e782525241847/] While fixing a problem I generally leave this command running to get the up2date log data [crayon-64832a520e783561602247/] mySQL Logs file with CPanel you can get hold of .err file here [crayon-64832a520e784928875630/] Configuration file can be located here [crayon-64832a520e785361692343/]     Apache Configuration File location httpd.conf can be found here [crayon-64832a520e786320462374/] You can viit like [crayon-64832a520e787130511390/]   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-64832a520e883273543819/] [crayon-64832a520e885164524633/] [crayon-64832a520e886310151772/] and put your desired port in as shown below [crayon-64832a520e887673306463/] You will need to allow your firewall to allow port 2224 for incoming All done then restart your sshd service as [crayon-64832a520e888156226525/] Now I wanted to rsync from my local server to my production VPS at port 2224 this is how I did it [crayon-64832a520e889219370155/] 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-64832a520e88a113227017/] 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-64832a520e9a1641983524/] and to extract the above package you use this   [crayon-64832a520e9a3952631699/] -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-64832a520e9a4837538846/] 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-64832a520e9b0994174172/] 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-64832a520eac9121625322/] [crayon-64832a520eacb496130053/] 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-64832a520eacc347383727/] -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-64832a520eb9b941521844/]     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-64832a520eb9d138415420/] 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...]