Skip to content

Instantly share code, notes, and snippets.

@JimmyCouch
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimmyCouch/9b9d483602ca92b88e86 to your computer and use it in GitHub Desktop.
Save JimmyCouch/9b9d483602ca92b88e86 to your computer and use it in GitHub Desktop.
Linux Gists for level up rails
* Everything is a file
1. Name some 'files' in a standard unix filesystem that aren't very file-like.
- Processes - startup process (init.d), deamon processes
- scripts/commands - SSH/Ifconfig - files that are available in the PATH, that execute code
2. Identify some places where we use file descriptors for non-file content as part of the development process (hint: sockets are a good example here).
- Sockets - FIFO stream of bytes between endpoints. not file content, but can be seen as a file
- Pipes - Again FIFO stream - Does not exist as a file, no process can open it. Instead processes share them and the calling method inherits the file descriptors
3. Postgres writes a file when it starts up to record some information about itself, called a 'pid file'. Find this file and display its contents.
- using file at /usr/local/var/postgres/postmaster.pid
7675
/usr/local/var/postgres
1408468394
5432
/tmp
localhost
5432001 1310720
* Path and Environment
1. Demonstrate the command to view all environment variables, and then use grep to find the one called EDITOR. If it's not set, modify your initialization scripts to set it to 'vim'.
- env
- env | grep EDITOR
2. Demonstrate a command that echoes the value of the PATH environment variable.
- echo $PATH
3. Demonstrate the command that will tell you the full path of the script used when you execute 'ruby'
- which ruby
* Transferring Files Between Boxes
1. scp jcouch.txt jcouch@10.0.0.1:~/tmp/
2. scp jcouch@10.0.0.1:~/tmp/jcouch.txt .
* File permissions
1. Use touch to create a new file in the /tmp directory. What permissions do user / group / and world have to that file by default?
- touch file.txt would have rw privileges of the user that created it, read permssions for the group and other
2. Use chown to change ownership of that file to the root user and root group.
So that we can continue to use it, use chmod to grant read and execute permissions to everyone for that file using the 'absolute' syntax.
3. We also want to remove the ability for the root group to write the file. Use the 'symbolic' syntax to remove that permission.
*SUUDOOOO
1. Name an example of a command where sudo is necessary to run a command.
- sudo vi /etc/sudoers
2. Name a few examples where use of sudo can get you into trouble.
- Oh, apache can't read from my rails public folder because apache runs under it's own user/group. Not very smart to chmod 777 from the root directory to /var/etc/www/public
* Monitoring System Resources
1. Demonstrate the command to check what the remaining disk space is on your laptop. Make sure to use the flag to make the output more readable.
- df -H
2. Navigate to the your home directory, and demonstrate the command to see how much space that directory and each subdirectory takes on disk.
- du -h
3. Demonstrate the command to see how much memory is currently being consumed on your laptop. Sort the running processes to see which is consuming the most resources.
- ps -Am
- top
4. Using that same command, run a few processor-intensive tasks on your laptop and observe the change to your processor usage for a few minutes. Read and interpret the CPU load numbers for the system.
* What's a Port and How to Find It
1. Demonstrate the command to show the list of currently listening ports on your laptop.
- netstat --listen
2. Do it again on a Linux VM, but make sure to include the flags to display the applications that are listening. Find the application port and pid that run sshd.
3. List the default ports for ssh, scp, http, https and postgres.
- ssh: 22
- scp: 22
- http: 80
- https: 443
- postgres: 5432
* Finding, pausing and killing processes
1. If you run the command `ruby -e 'sleep 1 while true'`, you'll start a ruby process that never completes. Try it out and verify that the command never returns. Use the keyboard command to kill it.
2. Try it again, but append the character that causes the process to run in the background.
&
3. Use the terminal command to bring it back to the foreground, then use the two keyboard commands to return it to the background. Yes this is a bit pointless, stick with me here.
fg/bg
4. List the processes on your system and find the PID that corresponds to that ruby script.
8042 ttys000 0:00.03 ruby -e sleep 1 while true
5. Since we don't want to repeat that debacle above, use the terminal command to rudely kill the ruby process based on its PID.
- kill -9 8042 //DESTROY!!!
* Starting and Stopping Services
1. On a Linux development VM, use init scripts to restart sshd. Y'know, just in case.
- /etc/init.d/sshd restart
2. Let's pretend you just changed your nginx config, and want to reload the changes without stopping the service altogether.. Invoke the init script without any arguments to see valid choices. Now run the correct init command to parse and load the config without stopping the server.
- /etc/init.d/nginx reload
* Monitoring Production Hardware
1. Name some resources that you might monitor for a production host.
- TCP traffic
- memory load
- processes (orphan processes)
2. There's a big problem with most monitoring: a momentary spike in resource usage isn't actually a problem. Some software, like the 'god' gem, tries to overcome this limitation. Can you think of some strategies to solve this problem?
- polling for processes every X minutes to check their mem usage, CPU usage, if they're stalling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment