The MagPi Essentials Conquer the commandline the Raspberry terminal guide Second Edition Raspberry download
The MagPi Essentials Conquer the commandline the Raspberry terminal guide Second Edition Raspberry download
https://ebookgate.com/product/the-magpi-essentials-conquer-the-
commandline-the-raspberry-terminal-guide-second-edition-
raspberry/
https://ebookgate.com/product/raspberry-pi-networking-cookbook-second-
edition-golden/
ebookgate.com
https://ebookgate.com/product/programming-the-raspberry-pi-second-
edition-getting-started-with-python-simon-monk/
ebookgate.com
https://ebookgate.com/product/raspberry-pi-user-guide-fourth-edition-
halfacree/
ebookgate.com
https://ebookgate.com/product/raspberry-pi-projects-for-kids-second-
edition-bates/
ebookgate.com
Raspberry Pi User Guide 2nd revised edition Edition Upton
https://ebookgate.com/product/raspberry-pi-user-guide-2nd-revised-
edition-edition-upton/
ebookgate.com
https://ebookgate.com/product/experiment-with-the-sense-hat-1st-
edition-edition-raspberry-pi/
ebookgate.com
https://ebookgate.com/product/halfacre-g-official-raspberry-pi-
beginner-s-guide-3rd-edition-gareth-halfacree/
ebookgate.com
https://ebookgate.com/product/raspberry-pi-robotic-projects-3rd-
edition-richard-grimmett/
ebookgate.com
2nd
EDITION
ESSENTIALS
CONQUER
COMMAND
TH
LINE
E
The Raspberry Pi
TERMINAL GUIDE
9!
01
Written by Richard Smedley
2 RS
R TE
FOAP
T ED CH
DA EW
P N
U +4
ESSENTIALS
LEARN | CODE | MAKE
AVAILABLE NOW:
GET THEM
DIGITALLY:
WELCOME TO
CONQUER THE
COMMAND LINE
ometimes only words will do. Graphic user
S interfaces (GUIs) were a great advance,
creating an easy route into computer use
for many non-technical users. For complex tasks,
though, the interface can become a limitation:
blocking off choices, and leaving a circuitous route
even for only moderately complicated jobs.
(Re-)Enter the command line: the blinking cursor
that many thought had faded away in the 1990s. For
getting instructions from user to computer – in a
clear, quick, and unambiguous form – the command
line is often the best way. It never disappeared on
UNIX systems, and now, thanks to Raspbian on the
Raspberry Pi, a new generation is discovering the
power of the command line to simplify complex
tasks, or instantly carry out simple ones.
If you’re not comfortable when faced with the $
prompt, then don’t panic! In this fully updated book,
we’ll quickly make you feel at home, and able to
find your way around the terminal on the Pi, or any
other GNU/Linux computer: getting things done, and
unlocking the power of the command line.
PUBLISHING
Publishing Director: Russell Barnes
Director of Communications: Liz Upton
CEO: Eben Upton
EDITORIAL
DESIGN Editor: Phil King
Critical Media: criticalmedia.co.uk Writer: Richard Smedley
Head of Design: Lee Allen Contributors: Lucy Hattersley,
Designer: Mike Kay Simon Long
In print, this product is made using paper This book is published by Raspberry Pi (Trading) Ltd., Maurice Wilkes Building, St. John's Innovation Park,
sourced from sustainable forests and Cowley Road, Cambridge, CB4 0DS. The publisher, editor and contributors accept no responsibility in respect
the printer operates an environmental of any omissions or errors relating to goods, products or services referred to or advertised in this product.
4 [ management
Chapter One ] which has been
system Except where otherwise noted, content in this book is licensed under a Creative Commons Attribution-
assessed as conforming to ISO 14001. NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0). ISBN: 978-1-912047-66-6
[ CONQUER THE COMMAND LINE ]
ESSENTIALS
CONTENTS
06 [ CHAPTER ONE ] 31 [ CHAPTER SIX ]
DON’T PANIC CONNECTING DISKS [ RICHARD
Take a look around Tackle the management SMEDLEY ]
and discover things of removable storage
[ Don’t
[ Contents
Panic ] 5
ESSENTIALS
ESSENTIALS
ONE ]
[ CHAPTER
DON’T PANIC In the first chapter, we take a look around and discover
that things aren’t as strange as they might appear…
6 [ Chapter One ]
[ CONQUER THE COMMAND LINE ]
t’s not a throwback to the past, but a quick and powerful way
I of getting your Raspberry Pi to do what you want, without
all that RSI-inducing menu chasing and icon clicking. The
command-line interface was a great step up from manually toggling
in your instructions in octal (base-8), using switches on the front of
the machine! Graphical user interfaces (GUIs) brought friendly visual
metaphor to the computer, losing some power and expressiveness.
With the Raspberry Pi, you can get the best of both worlds by knowing
[ READ THE both: after reading through this guide, you’ll soon be as comfortable
MANUAL ] at the command prompt as you are at your desktop.
Unlike some earlier versions of Raspbian, Stretch boots you straight
Help is included,
with man(ual) to a GUI, although you can change this behaviour in the settings. The
pages, but they command-line environment is still there: hold down the ALT+CTRL
can be a little
keys and press F1 (the first function key on the keyboard), and you’ll
overwhelming.
Use them to arrive at a ‘virtual console’. Press ALT+F2 through to F6 and you’ll
check out some find five further consoles waiting for you to log in.
extra options
You can drop into these any time you like, but for now press
beyond the
switches like ALT+F7 and you’ll be back in mouse and menu land. The command
-a we use here. line is also available through a program called a terminal emulator
To read the ls
(often referred to as a term or xterm). You’ll also find people referring
man page, type
man ls. to the shell, or Bash. Don’t worry about that for now; just click on the
icon at the top of the screen that looks like a black television screen,
The command
line is only a click
away: it is called
Terminal and you
can find it under
Accessories in
the menu
Commands are
terse, but, once
learned, they’re
a quick way of
navigating and
reading your files
and folders
[ Don’t Panic ] 7
ESSENTIALS
Fig 1 Switches
modify behaviour
in commands; ls
-a shows (dot) files
in your listing that
are usually hidden
from view
File path
You can list files and folders anywhere in your system (or other
connected systems) by providing the path as an argument to your
8 [ Chapter One ]
[ [CONQUER
CONQUERTHE
THECOMMAND
COMMANDLINE
LINE] ]
[ Don’t Panic ] 9
ESSENTIALS
10 [ Chapter One ]
ESSENTIALS
TWO ]
[ CHAPTER
READ/WRITE
TEXT
In this chapter, we get working on files
[ Read/Write
[ Don’t Panic
Text ] 11
ESSENTIALS
ow that we can navigate folders and list files, it’s time to learn
[ MORE
INFO ]
N how to create, view, and alter both files and folders. Once
more, it’s not that the task is difficult, rather that the forms
Many utilities of the commands (particularly when editing) are unfamiliar, coming
have info pages,
giving far more from an era before Common User Access (CUA) standards were created
information to ease switching between applications and operating systems.
than their man Stick with it: with just the first two chapters of this book under your
page. If you’re
feeling brave, try belt, you’ll be able to do plenty of work at the command line, and start
info nano for a getting comfortable there.
comprehensive
guide to nano.
Creating a directory
We’re going to dive straight into working with files and folders by
creating a new directory. Assuming you already have a terminal open
(see ‘Instant applications’ box), and you’re in your home directory
(pwd to check, cd ~ to get back there if necessary), type mkdir
tempfolder and have a look with ls.
mkdir, as you’ve probably guessed, creates a new directory or folder.
Let’s use it to experiment with altering one of the included Python
games. Don’t worry: we’re not going to be programming Python, just
making a small change by way of illustration. cd tempfolder (use
tab completion: cd t then hit the TAB key). In the following example,
we’ll be copying some files to this directory.
12 [ Chapter Two ]
[ [CONQUER
CONQUERTHE
THECOMMAND
COMMANDLINE
LINE] ]
cp /usr/share/python_games/fourinarow.py .
cp /usr/share/python_games/4row_* .
[ INSTANT
APPLICATIONS ]
Wildcard Although you
can open
The . (dot) at the end of the commands refers to ‘just here’, which the terminal
is where we want the files copied. Also, 4row_* is read by the Pi as emulator from
the menu –
‘every file beginning 4row_’ – the * is known as a wildcard, and this Accessories >
one represents any number of characters (including none); there are Terminal – for
other wildcards, including ?, which means any single character. this, and any
other app, just
Try python fourinarow.py and you’ll see you can run the hit ALT+F2
local copy of the game. To change the game, we need an editor – and type its
sidestepping the UNIX debate about which one is best, we’ll use the command name:
lxterminal.
Pi’s built-in editor: nano. Unless you’ve previously used the Pico
[ Read/Write Text ] 13
ESSENTIALS
14 [ Chapter Two ]
[ CONQUER THE COMMAND LINE ]
rm fourinarow.py
rm 4row_*.png
[ Read/Write Text ] 15
ESSENTIALS
[ CHAPTER THREE ]
PERMISSION
TO INSTALL We look at Raspbian’s efficient system for installing
and updating software, among other things
16 [ Chapter Three ]
[ CONQUER THE COMMAND LINE ]
Raspbian’s software
repository contains many
thousands of freely
installable apps, just a
command away from use
[ Permission to Install ] 17
ESSENTIALS
Fig 1 Raspbian
updates its listing
of thousands of
available apps,
providing you give it
admin permissions
Pseudo root, su do
We’ll get onto permissions properly a bit later, but for now you’ll be
pleased to know that you can fake it, using the sudo command. sudo
potentially offers a fine-grained choice of permissions for users and
groups to access portions of the admin user’s powers. However, on
the Pi, Raspbian assumes, quite rightly, that the default user will be
someone just wanting to get on with things, and sudo in front of a
command will pretty much let you do anything. You have been warned!
The following two commands will update Raspbian’s installed
software (Fig 1):
You can wait for one to finish, check everything is OK, then issue the
other command, or you can save waiting and enter both together with:
18 [ Chapter Three ]
[ CONQUER THE COMMAND LINE ]
Searchable list
It is possible to find particular apps with apt-cache search:
apt‑cache search games. You can also examine individual
packages with apt-cache show: apt-cache show bsdgames.
APT is actually a front end to the lower-level dpkg, which you can
call to see what you have installed on the system: dpkg -l. Even
on a fresh system, that’s a large listing: we’ll show you how to get
useful information from such listings another time.
Downloaded packages hang around in /var/cache/apt and if you
find yourself short on disk space, issuing sudo apt-get clean will
clear out the archive, without affecting the installed software.
Now, remember the extra details that ls -lh showed us in
chapter 1? Try ls -lh /etc/apt.
That -rw-rw-r-- at the beginning of the listing for sources.list
comprises file attributes, telling you who may use the file. Other
entries in the listing have a d at the beginning, indicating they are
directories. You’ll also see hardware devices have a c here, for character
device – ls -l on /dev/input, for example. On Linux, everything is a
file, even your mouse! A dash (-) at the start tells us this is just a regular [ FREE TO USE ]
file; it’s the remaining nine characters that cover permissions.
Software in
Every file has an owner and a group membership. Files in your home the Raspbian
directory belong to you. If you’re logged in as user pi and ls ~ -l, you’ll repository is
see pi pi in each listing, telling you the owner and the group. Note that not just free to
use, but freely
we put the switch at the end this time: that’s a bad habit under certain modifiable and
circumstances, but we’re just showing you what’s possible. Owner and redistributable.
group aren’t always the same, as ls -l /dev will show you. Free software,
like Raspbian’s
Debian base, is
File attributes built on sharing:
The file attributes, after the file type, are three groups of three for education
and for building
characters (rwx) telling you which users may read, write or execute the community.
file or directory for, respectively, the user who owns the file, the group
[ Permission to Install ] 19
ESSENTIALS
The id command
shows what
group access
you have, for
permission to
use and alter
files and devices
20 [ Chapter Three ]
ESSENTIALS
[ CHAPTER FOUR ]
MANIPULATING
TEXT
Discover pipes and learn how to connect multiple simple
commands together for more powerful text processing
[ Manipulating Text ] 21
ESSENTIALS
Building on simple
commands. The arrows
connect to streams and
files (input or output)
while pipes chain the
output of one program
to the input of another
22 [ Chapter Four ]
[ [CONQUER
CONQUERTHE
THECOMMAND
COMMANDLINE
LINE] ]
ls -l /usr/bin | wc -l
…will pass the long listing of the /usr/bin directory to the wordcount
(wc) program which, called with the -l (line) option, will tell you how
many lines of output ls has. In other words, it’s a way of counting how
many files and folders are in a particular directory.
grep if /usr/share/python_games/catanimation.py
[ Manipulating Text ] 23
ESSENTIALS
Fig 1 No matter
how long the
[ FILING file, grep will
HOMEWORK ] dig out the
lines you need.
There are many It’s also handy
more commands for finding the
beyond grep, results you want
from a multi-
sort and
page output
uniq that can
be chained One way to collect unsorted data is to combine lists. sort will put the
together. Take
a look at cut if combined listing back in alphabetical order:
you’re feeling
adventurous. ls ~ /usr/share/python_games | sort -f
File it away
Pipes are not the only form of redirection. > (the ‘greater than’ symbol)
sends the output of a program into a text file, either creating that text
file in the process, or writing over the contents of an existing one.
24 [ Chapter Four ]
[ CONQUER THE COMMAND LINE ]
Appending
If you want to add something to the end of a file without overwriting
the contents, you need >>.
…will read from list1, passing it to head to take the first two lines,
then putting these in a file called list2. Add in a pipe:
Lastly, let’s separate that stderr stream: it has file descriptor 2 (don’t
worry too much about this), and 2> sends the error messages to any file
you choose:
[ Manipulating text ] 25
ESSENTIALS
[ CHAPTER FIVE ]
CUSTOMISE THE
COMMAND LINE We make Raspbian a little more personal as we get it
to behave and look just the way we want it to
26 [ Chapter Five
One ]
[ CONQUER THE COMMAND LINE ]
The command- ake a look at that blinking cursor on your terminal, and at
line environment
is personal to
each user. You
T what’s behind it: pi@raspberrypi ~ $
The $ is known as the ‘dollar prompt’, awaiting your
can change your
identity with or command; before it you see the ~ (tilde), shorthand for ‘home’ – which
without a change is /home/pi in this case. Before that is [user name]@[computer name],
of environment,
depending upon in the form pi@raspberrypi. Not only is this informative (at least if
what you need to you’ve forgotten who and where you are), but it’s also something you
do in another role
can change and personalise.
New user
Let’s start with that user name: pi. If more than one person in your
family uses the Pi, you may want to keep the pi user for shared
projects, but set up individual login accounts for family members,
including yourself. Creating a new user in Raspbian is easy: sudo
adduser jo will create a new user account named jo. You will be
prompted for a password (pick a good one) and lots of irrelevant info
(dating back to shared university computers of the 1970s) that you can
safely ignore by just pressing ENTER at each prompt. Now we have
a user account for jo, have a look at /home/jo. Does it look empty?
Use ls -A. Jo has never logged into the computer, so you will see the
absence of most of the contents of /home/pi for now, but there is a
.bashrc and a couple of other config files.
28 [ Chapter Five ]
[ CONQUER THE COMMAND LINE ]
Transformations in the
virtual world are always
easier than those in nature
something you may have reason to do in the future – there are
interesting alternatives available for extra features or for smaller
memory footprint – but let’s not worry about that for now. You can [ BASIC
put all sorts of commands in there to personalise your environment: ACCOUNT ]
command aliases are great for regularly used combinations. adduser creates
a new user, then
Alias takes care of
all of the extra
As user pi, see what’s there with: grep alias ~/.bashrc. There are details like
a few aliases already in there, particularly for the ls command. One making a home
directory. If all
entry is: # alias ll='ls -l'. This sounds quite useful, although the
you want is a
# indicates that it is ‘commented out’, which means that it will not be user created
read by Bash. Open .bashrc in your text editor (double-click the file in with no extra
frills, then the
File Manager after pressing CTRL+H to show hidden files) – the simple
command you
Text Editor will do for now as although we’ve touched on using nano want is useradd.
for editing text from the command line, we aren’t going to go into this
in detail until the next chapter. Removing the # will mean that now
[ WHO AM I? ] when you type ll, you’ll get the action of running ls -l. Handy,
but we could make it better. Change it to: alias ll='ls -lAhF' and
From a virtual
console (CTRL+
you’ll get an output in kB or MB, rather than bytes, along with trailing
ALT+F1 to F6), su slashes on directory names and the omission of the ever present .
and that’s who and .. (current and parent) directories. Changes take effect after you
you’re logged
in as. From
next start a Bash session, but then you can just run that alias as a
an xterm, you command (Fig 1). To disable an alias for a session, use: unalias ll.
can change to
someone else,
but start another
Key point
app from the We’ll end with the very first thing many users need to change:
menu and you’ll the keyboard map. The system-wide setting is found in
be back to your
original login.
/etc/default/keyboard, but often you need to change it just for
individual users. If £ signs and letters without accents are not
sufficient for them, log in as the user who wants a different
keyboard, or add sudo and the correct path to the commands below.
For example, for a Greek keyboard:
touch ~/.xsessionrc
echo "setxkbmap el" > ~/.xsessionrc
Replace el with pt, us, or whatever language you desire. Note that
the config file we created – .xsessionrc – holds settings that are read
when we log in to the GUI, so the keyboard setting will cover not just
the terminal, but every app used in the session.
Fig 1 Those
terse, two- or
three-letter
commands are
not set in stone:
make your own
shortcuts to
keep, or just
for use over a
specific session
30 [ Chapter Five ]
ESSENTIALS
[ CHAPTER SIX ]
CONNECTING
DISKS
For chapter six, we’re tackling the
management of removable storage
[ Connecting
[ Don’t Panic
Disks ] 31
ESSENTIALS
Raspbian, while
presenting a simple
surface, also lets
you dig deep for
information when you
need to change default
behaviour. That’s real
user-friendliness!
Even simple
lthough Raspbian will, when booted as far as the GUI,
A
utilities have
multiple uses: df, automatically mount any disk-type device (USB flash key,
by showing space
available, reminds camera, etc.) plugged into the USB port and offer to open it
the user which disks for you (Fig 1, overleaf), you may wish to get more direct control of the
are mounted and
can be accessed process. Or, as is more often the case, you may want to mount a disk
by the Pi when the Raspberry Pi is running a project that doesn’t involve booting
as far as the GUI, as it’s not necessary for most sensor projects.
Connected or mounted?
Plugging a drive or flash memory device into your Pi (connecting it to
[ IN DEPTH ] your computer) is not the same as making it available for the Pi to
If you want to interact with (mounting it) so that Raspbian knows what’s on it and
delve deeper into can read, write, and alter files there. It’s an odd concept to accept: the
what goes on
inside Raspbian
computer knows there’s a disk plugged in, but its contents remain
and other GNU/ invisible until the Pi is told to mount it. It’s a bit like seeing a book on
Linux systems, your shelf, but not being allowed to open or read it.
try Brian Ward’s
excellent
Disks and disk-like devices are mounted by Raspbian on a virtual file
book, How system, and you’ll rarely need to worry about what goes on beneath
LinuxWorks that layer of abstraction, but to see some of it, type mount. The
(magpi.cc/
ZEhaBF).
information displayed is of the form device on mount point, file-system
type, options. You’ll see lots of device ‘none’ for parts of the virtual
32 [ Chapter Six ]
[ CONQUER THE COMMAND LINE ]
system that you don’t need to worry about; the devices that concern us
[ DISK &
start with /dev/ and have names like /dev/mmcblk0p1 for partitions DISK SPACE ]
of the Pi’s SD card, and /dev/sda1 for plugged-in USB drives.
Plug in a USB drive (a flash drive should work fine, but some hard The df
command shows
drives may require a separate power supply). Like most computer you space on
desktops, Raspbian automatically mounts the disks, so (unless mounted drives:
you boot to a virtual console) you’ll need to unmount it. mount will just type df and
you’ll also get a
show an entry beginning something like /dev/sda1 on /media/pi/ list of connected
UNTITLED… and you can unmount with sudo umount /dev/sda1 drives. It’s more
(yes, that is umount without an ‘n’). An error will result if the device readable than
mount -l, though
is in use, so change directory and/or close apps using files from the lacking file type
device. Now we can mount it just the way we want. info. It’s also
quicker to type!
[ Connecting Disks ] 33
ESSENTIALS
Fig 1 Raspbian
wants to mount
plugged-in disks,
and take care of
the details for
you – note that
the GUI tells you
it’s ‘Windows
software’ – while
the command
line beneath has
information for
you to take control
when you need
the job done in
a particular way,
telling you it’s an
NTFS file system
File-system table
Raspbian knows which disks to mount at boot time by reading the
file-system table (/etc/fstab), and we could put our /dev/sda1 in
there, but if we start up with two drives plugged in, the wrong one
may be selected. Fortunately, disks (or rather, disk partitions) have
unique labels known as UUIDs randomly allocated when the partition
is created. Find them with sudo blkid, which also helpfully tells you
the label, if any, that often contains the make and model of external
drives, or look in /dev/disk/by-uuid.
For an NTFS-formatted drive, we called sudo nano /etc/fstab and
added the following line to the end of the file:
/dev/disk/by-uuid/E4EE32B4EE327EBC /media/usb
ntfs defaults 0 0
34 [ Chapter Six ]
[ CONQUER THE COMMAND LINE ]
[ Connecting Disks ] 35
ESSENTIALS
ESSENTIALS
[ CHAPTER SEVEN ]
PREDICTABLE
NETWORKING
In this chapter, we give the Raspberry Pi a permanent
network address of its own
36 [ Chapter Seven
One ] ]
[ CONQUER THE COMMAND LINE ]
The ifconfig
command tells
you information
about your
Raspberry Pi’s
network address
[ Predictable Networking ] 37
ESSENTIALS
section if you are connected via Ethernet, or under the wlan0 section if
you’re connected wire wireless LAN.
A faster way to get your IP address is to enter hostname -I on the
command line.
38 [ Chapter Seven ]
[ [CONQUER
CONQUERTHE
THECOMMAND
COMMANDLINE
LINE] ]
interface wlan0
static ip_address=192.168.0.2/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8
Save the file with CTRL+O and then exit nano with CTRL+X.
[ Predictable Networking ] 39
ESSENTIALS
Normally you don’t want your computer set to use a static IP address.
You can change the network configuration back by editing dhcpcd.conf
again using sudo nano /etc/dhcpcd.conf. Remove all the lines you
added in the previous step, then save the file again.
Ping!
Ping is the most basic tool in the network testing armoury, but one
which is often called upon. It sends an ICMP (Internet Control Message
[ DOMAIN NAME
SERVERS ] Protocol) ECHO_REQUEST to a device on the network. ICMP is built into
every connected device and used for diagnostics and error messages:
We added a ping will produce a reply from the pinged machine, which tells you
192.168.0.1
8.8.8.8 to
it is on, and connected, and that the network is working between you
our domain_ and it. Information about packets lost, and time taken, also helps with
name_servers fault diagnosis.
line in /etc/
dhcpcd.conf.
A successful ping localhost from the Raspberry Pi tells you not just
This is for the that the local loopback interface is working, but that localhost resolves
Google public to 127.0.0.1, the local loopback address. Name resolution is the cause of
domain name
server (DNS). The
many computing problems – see ‘Domain Name Servers’ box. Now ping
DNS maps public the Pi from another machine on your local network: ping 192.168.0.2
IP addresses like (Fig 2) – you’ll need to use the static IPv4 address you set, rather than ours,
raspberrypi.org
to IP addresses
of course. If you’re doing this from a Windows machine, ping defaults to
(in this case five attempts; from another UNIX machine (another Pi, a Mac, or Ubuntu
93.93.128.230). or other GNU/Linux), it will carry on until you stop it with CTRL+C, unless
You’ll need
to add a DNS
you set a number of ECHO_REQUEST sends with -c like so:
reference to
access websites ping -c 5 raspberrypi.org
in a browser.
IPv6
The four-digit IP address style we use (such as 192.168.0.2) is IPv4.
A newer standard, IPv6, is becoming more common. These are
longer 128-bit addresses represented in hexadecimal (for example,
fd51:42f8:caae:d92e::1). Look at the example code in dhcpcd.conf for
setting up a static address with IPv6.
40 [ Chapter Seven ]
ESSENTIALS
[ CHAPTER EIGHT ]
STOPPING A
PROCESS
As close to perfect as Raspbian is, things can go wrong . In this chapter,
we learn that there’s no need to turn the Raspberry Pi off and on again:
just kill the process!
[ Stopping
[ Don’t Panic ]
A Process 41
ESSENTIALS
Programs running in
the terminal can be put
to sleep by sending to
the background – from
where they can easily be
brought back with fg
Keep an eye on your
processes, and you’ll also
be able to see what’s
hogging the Pi’s CPU and
memory resources
Processes
Find the many processes running on your Pi with the ps command. As
a minimum, on Raspbian, it’s usually called with the a and x switches –
which together give all processes, rather than just those belonging to a
user, and attached to a tty – and with u to see processes by user; w adds
wider output, and ww will wrap over the line end to display information
without truncating.
Type ps auxww to see, then try with just a or other combinations.
You will notice that these options work without the leading dash
seen for other commands. Both the lack of dashes, and the particular
letters, a and x, date back to the original UNIX ps of the early 1970s,
maintained through various revisions by one of UNIX’s two family
42 [ Chapter Eight ]
[ CONQUER THE COMMAND LINE ]
branches, BSD, and baked into the first GNU/Linux ps. UNIX’s other
branch, System V, had extended and changed ps with new options and [ KEEP ON TOP ]
new abbreviations for command switches, so for ps ax you may see When using a
elsewhere ps -e (or -ef or -ely to show in long format). virtual console,
The ps aux listing has various headers, including the USER which it can be worth
keeping htop
owns the process, and the PID, or Process IDentification number. This running so that
starts with 1 for init, the parent process of everything that happens in if there are any
userspace after the Linux kernel starts up when you switch the Pi on. problems, you
can CTRL+ALT-
Knowing the PID makes it easy to kill a process, should that be FN there for a
the easiest way of shutting it down. For example, to kill a program quick look for any
with a PID of 3012, simply enter kill 3012, and to quickly find the problems – even
if the GUI freezes.
process in the first place, use grep on the ps list. For example, locating
vi processes:
ps aux | grep -i vi
Piping commands
Naturally, you can pipe ps’s output to select the PID and feed directly
to the kill command:
We don’t have space for an in-depth look at awk (we’re using it here
to print the second field of grep’s output: the PID), but the [f] trick
at the beginning of Firefox (or whatever named process you want to
kill) prevents the grep process itself being listed in the results; in the
vi example above, grep found the grep process itself as well as vi (and
anything with the letter sequence vi in its name).
The output of ps also shows you useful information like percentage
of memory and CPU time used, but it’s more useful to see these
changing in real time. For this, use top, which also shows total CPU
and memory use in the header lines, the latter in the format that you
can also find by issuing the command free. For an improved top:
[ Stopping A Process ] 43
ESSENTIALS
[ QUICKER BOOT ]
sudo apt-get install htop
The start up
process of
htop is scrollable, both horizontally and vertically, and allows you
Raspbian
Wheezy is to issue commands (such as k for kill) to highlighted processes. When
controlled by the you’ve finished, both top and htop are exited with Q, although in htop
traditional SysV
you may care to practise by highlighting the htop process and killing it
init. Raspbian
Jessie, like other from there (see Fig 1). htop also shows load over the separate cores of
GNU/Linux the processor if you have a Pi 2 or 3.
distributions,
has moved to
the faster (but Background job
monolithic) Placing an ampersand (&) after a command in the shell, places the
SystemD – we
program in the background – try with: man top & and you’ll get an
touch on some of
the differences in output like: [1] 12768.
chapter 11. The first number is a job number, assigned by the shell, and the
second the PID we’ve been working with above. man top is now running
in the background, and you can use the job control number to work with
the process in the shell. Start some other processes in the background
if you wish (by appending &), then bring the first – man top – to the
foreground with fg 1. Now you should see man running again.
Fig 1 htop tells you
You can place a running shell program in the background by
what’s running, ‘suspending’ it with CTRL+Z. fg will always bring back the most
what resources
recently suspended or backgrounded job, unless a job number is
it’s using, and lets
you interact with specified. Note that these job numbers apply only within the shell
the process, even
where the process started. Type jobs to see background processes;
killing htop from
within htop jobs -l adds in process IDs (PID) to the listing.
Signals
When we send a kill signal from
htop, we are given a choice
of signal to send. The most
important are SIGTERM, SIGINT,
and SIGKILL.
The first is also the signal kill
will send from the command line
if not called with a modifier: it
tells a process to stop, and most
programs will respond by catching
44 [ Chapter Eight ]
[ CONQUER THE COMMAND LINE ]
Note that the nohup.out log file created here will need sudo
privileges to read – or reassign with:
[ Stopping A Process ] 45
Another Random Document on
Scribd Without Any Related Topics
therefore referred it to me to give your Highness an account of what
was upon their hearts;—which was that if there was not a restraint
upon them, they had cause to fear they would put all into blood and
confusion, their party giving out they would down with the Major-
Generals, and Decimators, and the new militia, so that they do
apprehend it good for your Highness to consider beforehand what is
fit to be done. And therefore they think it necessary to appoint some
persons that shall have commissions dormant to some gentlemen,
that the honest party may know to whom to repair, and to that end,
at their next meeting, shall present you with names accordingly.
They think a recognition, as it may be penned, may keep some that
are most dangerous out, and better that they be kept out at first,
than afterwards your Highness be forced to turn them out. They
think it will be your wisdom not to suffer them to meddle with the
instrument of Government, but all that go into the House be
engaged to own it as it is, and not to meddle with altering any part
of it, without your Highness's consent. As also not to meddle with
what hath been done out of necessity by your Highness and Council,
in order to the peace and safety of the nation. Their hearts are with
your Highness to stand by you with their lives and fortunes, they
finding such a perverseness in the spirits of those that are chosen,
that, without resolution of spirit in your Highness and Council to
maintain the interests of God's people (which is to be preferred
before a thousand Parliaments) against all opposition whatsoever,
we shall return again to our Egyptian taskmasters. And therefore do
earnestly beg that the Lord would direct your heart what to do in
this juncture of affairs. And if Parliaments will not do it, then to take
such to your assistance as will stand by you in that work, which God
hath begun, and will yet undoubtedly own and carry on, maugre all
His enemies; and we judge it better to persist in the work of the
Lord than now we have put our hands to the plough to look back.
And although the murmurings and discontents of God's people,
together with all our unsuitable walkings under those precious
enjoyments we have from the Lord, may provoke Him to leave us to
be overcome by our enemies, and cause us to hang our harps upon
the willows, and cause the enemy to call for one of the songs of Sion
in a strange land; yet, if the Lord shall take pleasure in us, He will
cause His face to shine upon us, and carry us well through the seas
of blood that are threatened against us, and the waste howling
wilderness of our straits and difficulties, and at length bring us to
that blessed haven of reformation, endeavoured by us, and cause all
our troubles and disquiet to end in a happy rest and peace—when all
His people shall be one, and His name one in all your dominions,
which is and shall be the daily prayers of, my Lord, your Highness's
most humble and obedient servant to his power,
"Thos. Kelsey.
"Chatham, 26th August, 1656."
This advice was adopted, and between one and
two hundred of the persons returned were refused 1656, September.
their seats because of their disaffection to the
Protectorate Government.
The second Protectorate Parliament met on the
17th of September, 1656. Sir Harry Vane, now a Cromwell's
Speech.
prisoner, had been proposed in three places, but
had been elected in none. Haselrig had succeeded in securing his
return, but for a time he did not take his seat. After Dr. Owen had
preached at Westminster Abbey from the words in Isaiah xiv. 32
—"What shall one then answer the messengers of the nation? That
the Lord hath founded Zion, and the poor of his people shall trust in
it,"—adjourning to the Painted Chamber, Cromwell stood up, took off
his hat, and discoursed characteristically upon the Spaniards and
Papists, and the Cavaliers—upon the late rising—the levellers and
the Fifth Monarchy men—and also upon the Major-Generals. Then
he turned to the subject of religion. His practice since the last
Parliament, he said, had been to grant liberty to all who continued
quiet and peaceable. He was against such liberty of conscience as
might be repugnant to this. Let Baptists, Independents, and
Presbyterians be countenanced as long as they were thankful to
God, and made use of their liberty—not to interfere with others, but
"to enjoy their own consciences." Men who believed in free
justification by the blood of Jesus, and lived upon the grace of God,
claimed freedom as a debt due to God and to Christ; and God would
require it, if such Christians did not enjoy what they claimed. But his
Highness declared he would not suffer one Christian to trample on
the heels of another, or to revile, reproach, or provoke him. He
prayed that God would give hearts and spirits to keep things equal,
for striving after which he had "some boxes on the ear." Even
Presbyterians, at last, were beginning to see the justice of his
course, and petitions from them in certain counties shewed how
they did but desire liberty, and would "not strain themselves beyond
their own line." The Protector touched on another topic. For his own
part, he should think himself very treacherous if he took away tithes
till he could see the legislative power settle the maintenance of
ministers in another way. To destroy tithes was to cut ministers'
throats. Tithes, or some other public maintenance, formed "the root
of visible profession." He had also a word of favour for his
Commission of Triers and "Expurgators." They had a great esteem
for learning; but "neither Mr. Parson, nor Doctor in the University,
hath been reckoned stamp enough by those that made these
approbations." Grace must go with and sanctify learning. He
believed, he said, that God had "a very great seed" in the youth then
in the Universities, who, instead of studying books only, studied their
own hearts. "It was never so upon the thriving hand" as at that day.
Touching upon religion generally, the speaker added that the
Cavalier interest had been one of disorder and wickedness; that
fifteen or seventeen years before it had been a shame to be a
Christian. A badge then was put upon the holy profession. But a
blessed change had come, and now—since people esteemed it a
shame to be bold in sin and profaneness—God would bless them.
[127]
The old difficulty, how to make people pay their tithes—not yet
overcome by all the legislation on the subject in the years 1647 and
1648—presented itself to this Parliament. Cromwell's Council books
afford numerous instances of ministerial complaints respecting
arrears of income. Orders promptly made are recorded; but
subsequent complaints indicate how the execution of these orders
must have been resisted. For example, it was directed, in 1654, that
an augmentation of the chapelry of Brentford, Middlesex, by a
charge on the tithes of the Rectory of Ashwell, in Hertfordshire, up
to that time paid in corn, should be paid in money; and the Lord
Protector recommended that the income should be increased to
£100 per annum. The same augmentation became afterwards
charged on other Rectories; and, in the year 1657, his Highness and
the Rector of Hanwell (Brentford being in Hanwell parish), appointed
Abriel Borfett to the Brentford chapelry. Yet, after all these repeated
arrangements, petitions for payment of arrears abundantly prove the
difficulty which existed in the way of enforcing the claim.[146]
On the 1st of June, 1657, Sir William Strickland,
member for Yorkshire, moved the first reading of a 1657, June.
new tithe Bill; and, upon Whitelocke's objecting to
a clause in it authorizing ministers or their agents to enter men's
houses to enforce payment, as a thing never granted even in times
of Popery—no man having ever heard of a distress for tithes—the
mover replied that he was afraid some persons had a design, by
bringing disgrace on the system, to dishonour the Gospel; that there
were men who would leap over hedge and ditch, and over the whole
decalogue, and then scruple about tithes, and never willingly pay
them; that some severity was needful to preserve Church revenues;
and that the same principle which endangered one kind of property
imperilled all the rest.[147]
The Presbyterians were zealous in catechising
their children. The Provincial Assembly of London Catechising.
had passed a series of resolutions on the subject in
1655,[148] and now an attempt was made to legislate upon the
subject; but when a Bill for this purpose was introduced on the 9th
June, 1657, Major-General Desborough moved that it be "left
behind," since it would "discontent many godly persons and make
them mourn." Others spoke in the same strain, but Mr. Vincent,
member for Truro, and Colonel Briscoe, begged on their knees, that
the House "would not forbear the Bill," in which earnest and
impassioned plea they were supported by so large a majority of
Presbyterian members that, on a division, the yeas were 82, and the
noes but 7. "So," as the Journals record, "it was resolved that the Bill
for catechising be now carried up." It was carried up with several
other bills: whereupon the Speaker made a short speech to his
Highness "relating to the slowness of great bodies moving, and how
our fruits were like that of the harvest, not all ripe at a time, but
everything in its season; and how he hoped that this was but the
vintage to the autumn the Parliament was preparing, and that it was
not with their productions as with Rebecca's births, where one had
another by the heel, but that their generation of laws was like that
of natural generation, and that his Highness was the sun in the
firmament of this Commonwealth, and he must give the ultimate life
and breath to our laws."
Thirty-eight of the Bills received the Protectoral
assent, but the thirty-ninth, the Bill for catechising, 1657, June.
met with a strange fate. After a little pause, his
Highness, looking at the parchment before him, said, "I am desirous
to advise of this Bill." Hence the Bill dropped. This being done, the
House returned about two o'clock to report proceedings, when Mr.
Bampfield, member for Exeter, standing by the table, declared "that
his Highness never did himself such an injury as he had done that
day." Mr. Scobell, the clerk, told Mr. Bampfield he ought not to talk
so, but the stiff Presbyterian declared "he would say it anywhere."
[149]
ebookgate.com