Archive for the ‘Linux Tips’ Category

.NET Core Bootstrap Script for Linux

A quick script to bootstrap a dotnet core development environment for Linux Mint 18 / Ubuntu 16.04. Installs the following components.

  • .NET Core 1.1
  • Visual Studio Code with C#
  • Node.js and NPM
  • Yeoman

Usage

1
curl -s https://gist.githubusercontent.com/tschoonover/274627440d11ff32c2cd91a9d16a4974/raw/b86a6dfb39a06e49882ce8f6c698993cf8b993fd/bootstrap-dotnet | sudo bash -s

Automatically ending a process in Linux

I have a Python script that I want to run from one console, and sometimes I want to be able to kill it from another console login (via SSH or direct to the console). To do this easily I simply wrote a bash script that uses grep and awk and then passes the results to a ‘sudo kill’ command like this:

sudo kill $(ps aux | grep 'sudo python script1.py' | awk -v i=1 -v j=2 'FNR == i {print $j}')
sudo kill $(ps aux | grep 'python script2.py' | awk -v i=1 -v j=2 'FNR == i {print $j}')
cp /var/log/security_camera.log ./

However, if you have a particularly stubborn script, you may need to use the KILL signal. To do that just specify the KILL signal like this:

sudo kill -s KILL $(ps aux | grep 'sudo python script1.py' | awk -v i=1 -v j=2 'FNR == i {print $j}')
sudo kill -s KILL $(ps aux | grep 'python script2.py' | awk -v i=1 -v j=2 'FNR == i {print $j}')
cp /var/log/security_camera.log ./

Only use the latter if you absolutely have to. There are actually a lot of signals in between the default SIGTERM signal. See:

man kill

to see the system documentation on which signals you should use when. Or just Google it. 🙂

As with most things in Linux, this is only one way to do things. But it works great for me.

This post was inspired by this Linux comic I saw on Google+ ✌  The code is all written by Leland Green (me). I take absolutely no responsibility with what you’re about to do with it! 

Jasper on Raspberry Pi Running Raspbian Jessie

This will get Jasper running on your Pi. This method really works! I’ve tested this and got it going on an RPi 2 running Raspbian Jessie.

The second script* prompts you for essential information and then it’s up to you to configure and use it.

All you need to do is download the two .sh files from my new project here: https://github.com/lelandg/Jasper-Installation-on-Raspbian-Jessie

See the repository for much more info Please report any errors that you may find there.

😎👍

*Technically it runs a Python script that prompts you for the info… but it appears as though it happens as described above. It’s all smoke and mirrors anyway, right? So everyone expects a little magic now & then.🙂

Bash By Example

If you’d like to learn Bash programming, there are few resources better than the IBM DeveloperWorks site. They have an awesome series of tutorials here:

Bash By Example:

Part 1: https://www.ibm.com/developerworks/library/l-bash/
Part 2: http://www.ibm.com/developerworks/library/l-bash2.html
Part 3: http://www.ibm.com/developerworks/library/l-bash3.html

See Also:

GNU’s bash home page

Parallel Processing In Python the Easy Way!

The technical term for what we’re discussing today is Decorated Concurrency. Don’t let this scare you! 😇 It’s just a fancy (and concise) way of saying that, “We have a simple way to do parallel processing.”

Parallel processing, in turn, means that we can start multiple threads, as needed, in our program such that everything runs faster. Behind-the-scenes, on modern processors that have multiple cores, Python can run different processes on different cores. This lets a developer decide which processes will run the fastest because they have their own core. Or, at a higher level, which functions/methods are candidates for running on multiple cores.

I’ve known about parallel processing in Python for some time but it has always required making special function calls. No big deal – especially since you have to write the code to be multithreaded in the first place – but it would be nice if it was all a bit easier, right?!? Well now it is!!! 😎

The Deco (deco) project allows you to easily create code that takes advantage of parallel processing in Python. You simply do it with decorators!

If you’re new to programming, a decorator is just something that you place in your code next to a function/method/class, etc. definition. For example, the Deco project uses

@concurrent

and

@synchronized

tags. In Python you apply a decorator immediately before a function/method, etc. You can see some examples in the README.md file (that is shown to you by default) in this project: https://github.com/alex-sherman/deco (same link as above)

I expect you will be able to see some examples and some of my code very soon, as well. 😇

🙂👍

Bonus Linux tip: 

How to Isolate a CPU (core) and dedicate it to a specific task

To run a task on core 3 of a Raspberry Pi, or other Debian-based distro, including Ubuntu, simply add:

isolscpus=3

to the end of /boot/cmdline.txt. (There can be only one line in this file.)

And then run:

taskset -c 3 <your task here>

BTW, if you get a chance, please check out my new blog! http://lelandgreen.com
And if you like it, will you please share it around? 😎👍

Section9 Linux tip: aliases for installing and uninstalling packages (and related)

This will be a simple post. I just wanted to share some useful things to make installing and maintaining packages much simpler (not that apt is bad).

To enable these features simply open a console window and:

sudo nano ~/.bashrc
*Note: You may not always need “sudo” to edit this file, but on some systems you may.

Scroll to the end of the file and add these lines, exactly like this:

alias install='sudo apt-get install -y'
alias purge='sudo apt-get purge -y'
alias autoclean='sudo apt-get --autoclean'
alias show='apt-cache show'
alias search='apt-cache search'

Finally, type Ctrl-X and when prompted to save type ‘y’ and then press <Enter> at the next prompt. You will then need to log off and back on, or else type:

. ~/.bashrc

BONUS TIP: Beginning a line with ‘.’ lets you run shell and startup scripts

Also be aware that you may see errors if you run your .bashrc like this. They should be safe enough, but if you experience problems, log off and back on or reboot.

You can make sure you have it setup correctly by typing:

install

And pressing <Enter>. If you get a “command not found” error then you did not get the script to execute. Go ahead and reboot in this case and it should work from then on. 🙂

This allows you to do things like:

install [someprogram]
purge [someotherprogram]
autoclean
show [package_or_program_name]
search [what_was_that_program?]

When using this search command, if you do not see any matches to your “search”, try a simpler search. You can use parts of words. And you can still type –help as a parameter to any of these new shell commands and it will show you the appropriate help screen. They really are just aliases: you still run the program they point to. In other words, for the search command, you really are running apt-cache search such that if you pass it –help, you should get help on apt-cache search features (and apt-cache in general). In this case we really are running apt-cache.


 

I hope this little introduction to using aliases for apt is instructive and/or helpful. If you think so, I hope you’ll subscribe to this blog, either with RSS or good-ol’ email. Or simply follow me (Leland Green) on Google+ or Facebook (and soon on my own web site, I hope).

Thank you for reading,
Leland…

Section9 Linux Tip: ls

ls

Take some time to learn some of the options available with this powerful utility that does more than just “list files”, including:

  • -R = Recurse directories.
  • -l = “Long” listing–show file sizes, timestamps and permissions.
  • –color = Colorize output, or disable colors with ‘ls –color=never’.
  • -a = All files–show everything, including “hidden” files (those beginning with a ‘.’ (period)).
  • -A = Almost all files. Like -a but does not show ‘.’ or ‘..’ shortcuts (since they are always present).
  • -S = sort output (see ‘ls –help’ for details on parameters for sorting, or use the individual “sort switches”, such as:
    • -t = sort by timestamp
    • -S = sort by size
    • -v = sort by version
    • -X = sort by file extension

Given all of that, can you guess what my favorite alias command for ls is? I’ll give you a hint: it’s one of these (from my ~/.bashrc file):

alias ll='ls -l -a --color'
alias la='ls -A --color'
alias l='ls -CF --color'
alias l1='ls -1 -a --color=never'

Note: That last one is lowercase ‘L’ and the digit ‘1’ (one).

How do you use ls? Please share in the comments below. I will acknowledge all ideas you give me (and, in fact, by leaving a comment you can “seal it in stone”).


If you found this page useful or interesting, please stay tuned to the “Linux Tips” tag. We here at Section9 always appreciate any shares and/or links to our pages. Link to one of our pages on your site, then post a comment here with a link back to your page that you put the link on. In this way you can promote your website on Section9.space (and we encourage you to do so).

I would also appreciate reports of any errors, typos, mis-statements and anything else you care to nit-pick. 🙂 Leave me a comment here (preferred), or send a private message (PM) on any of the social media sites that you find me on. (Google+, Facebook, Section9.space, Twitter, Instagram, etc.) I try to always at least acknowledge all questions within 24 hours. If I don’t know the answer, I’ll either google it before replying, or at sometime after that, depending on how difficult I think a true answer will be to write.)

Thank you for your interest,
Leland Green…


Contact me with a private message:


Or, better yet, leave a public comment:

Section9 Linux Tip: screen

Let’s start the Linux Tips series with a command for everyone:

  • screen

This a cool program that lets you load multiple terminal sessions on one Linux machine, all within one window.

You can use:

screen -r

to reconnect to your previous session if the connection is dropped (but the Linux box is still running). Then *all* your sessions are still open! (When we ran Linux servers at work we were required to do this for a period of time, and I came to love screen!

Create a new session:

<Ctrl>-A <Ctrl>-C

Switch to the “next” session with:

<Ctrl>-N

Use one window, ssh to your “primary” host, run screen and then open an additional session for each machine you want to SSH to, then just ssh to each machine in its own screen session.

In my ~/.bashrc I have this line to make a new “command” named ‘scr’:

alias scr='screen -A -h 9999 -O -q'

Then I can just start it with ‘scr‘ or ‘scr -r‘ if I want to reconnect to a already-running session. (You may want other options, too. See what’s available by running ‘scr –help‘.


If you found this page useful or interesting, please stay tuned to the “Linux Tips” tag. We here at Section9 always appreciate any shares and/or links to our pages. Link to one of our pages on your site, then post a comment here with a link back to your page that you put the link on. In this way you can promote your website on Section9.space (and we encourage you to do so).

I would also appreciate reports of any errors, typos, mis-statements and anything else you care to nit-pick. 🙂 Leave me a comment here (preferred), or send a private message (PM) on any of the social media sites that you find me on. (Google+, Facebook, Section9.space, Twitter, Instagram, etc.) I try to always at least acknowledge all questions within 24 hours. If I don’t know the answer, I’ll either google it before replying, or at sometime after that, depending on how difficult I think a true answer will be to write.)

Thank you for your interest,
Leland Green…

Return top

Info

Section9 is a computer club based out of the Springfield Missouri area. For more information, please see the About Us page or find us on Facebook.