Seperis (seperis) wrote,
Seperis
seperis

  • Mood:

pi plex server

Thanks to my birthday, I finally got to experiment with creating a Raspberry Pi Plex Server. I've only been dreaming of this for two years, so here we are.

If you don't know what Plex Media Server is, I wrote about it before, but it's pretty much the standard for home media servers. It's out of box easy to use with any operating system; it's all point and click; it can run on anything at all. It's free. Right now, it's the best option right now for usability, ease of access, and features, and honestly, it doesn't have a lot of rivals that match it on any of these and none on all fronts. Kodi's comes second, but by quite a bit; I tried it, I liked it, but it needs more seasoning and some understanding on how people use media servers.

This is, right now, the best place to put your movies, tv shows, and fanvids. Though with fanvids, some work will definitely be involved, it's worth it for the sheer organization and tagging and linking you can do to associated shows and movies.

It runs on literally anything, but as I got the pi up, if you don't have an old computer and want to do some comparison shopping, I thought I'd break down the price point on building an economy Home Media Server that can play anything--even 4K, I'm doing it now--and won't break your budget and how to install and run it.

Pi Plex Media Server: Hardware


We'll start with what you need for your new Pi Plex Media Server.

Raspberry Pi


Raspberry Pi is a single board computer you could wear as a necklace. The Pi 4 is the latest and fastest iteration; it has a quad processor with three RAM options: 1G, 2G, and 4G. The whole thing runs off an SD Card installed with NOOBS, which is where your operating system will be.

This shit is fun; if you've never in your life built a computer or even opened the case, this is for you. It's a fantastic introduction to the meat of a computer without the extras or intimidation. I promise you personally, you won't screw it up.

Raspberry Pi
Raspberry Pi, 4G - $61.91

Partial Specs
1.) CPU: Broadcom BCM2711, quad-core Cortex-A72 (ARM v8) 64-bit SoC@1.5GHz
2.) RAM: 4GB LPDDR4-2400 SDRAM
3.) 2 USB 3.0
4.) 2 USB 2.0
5.) 1 USB 3.0 Type C (For power supply only)
6.) 2 micro HDMI Ports
7.) Wifi: 2.4GHz and 5GHz
8.) Ethernet: gigabyte
9.) Bluetooth

You can hook up speakers, a camera, so many other things.

What you see is what you get, though: the board. You need power and a way to connect it to a screen and say, an operating system. This is easy.


Raspberry Pi: Required Components


Generally, especially when you first start out, don't bother--like I did--getting all the pieces separately; get a kit. I promise you, unless you have a ridiculous amount of time to do nothing at all but search the web for bargains, it's much, much cheaper. There are many kits, but you need the following in that kit:

Required Components:
1.) Power Supply for Raspberry PI - $11.99
2.) Case - any price from $4.99 but to get 3 and 4 below, probably minimum $7.99 up
3.) Fan - should come with case
4.) Heatsinks - should come with case
5.) micro HDMI to HDMI cable - $4.99 - $7.99
6.) microSD Card - $7.99

Total Price: $25 to $30 at lowest price point

(For three and four: don't panic, this shit is stick on and has graphics showing you where to put them on the board. Ten seconds, tops. I don't lie, you won't have a problem.)

Just because I can.

Cool Optional Components:
1.) Camera - $8.99 (just to start, they have fancier ones)
2.) 7' Pi Touchscreen case: $69.99

Like I said, though, don't bother with pieces now; unless you are building something that asks for specific parts--which is like about ten levels above this tutorial--get a kit and make your life easy. The parts you get with a kit are excellent.

My recommendation:
Cana Raspberry PI Starter Kit - $82.99. It comes with everything you need but the SD Card with NOOBS preinstalled; I promise, you can do that part yourself. Instructions are in the box on this, but I got this assembled in literally five minutes. Unpack everything, stick on the heat sinks, stick on the fan, put in case, close; that's it.

Why not the kit with the SD Card with NOOBS already in there? It's $17 more. SD Card alone? Six to seven dollars. You can get any brand of card, literally, just get one with decent speed. Sandisk, Samsung, etc.


Raspberry Pi: Installing NOOBS on a microSD Card


Now, getting NOOBS onto that SD Card: there are many tutorials on it, but it's ridiculously easy to do if you have ever in your life used an SD Card. This is a combination of them because some left out some key parts, like, format your SD card to FAT32, which your computer may not be able to do so you need to download something that will. So.

Link:
Based off this tutorial setting up your Pi, but for the SD card, do below.

Getting NOOBS on your micrSD Card
1.) Buy a microSD Card
2.) Download NOOBS from Raspberry Pi.
3.) Insert microSD Card in your computer.
4.) Download the FAT32 Formatter. The tutorial has a different option but this one is the one I read to use for FAT32 formatting on a Windows computer, so I got it.
5.) Format your microSD Card with it to FAT32.
6.) Extract NOOBS from zip file.
7.) Copy everything from extracted NOOBS to your SD Card.
8.) Done

Complete all the parts of putting together your Pi--the instructions are literally in the box, and if you have problems, IM me and I'll send you my phone number to call me--put the card in the Pi, plug into something HDMI to watch, hook up an extra keyboard and mouse, and follow the onscreen instructions.

The entire operation of taking it from the box to seeing the welcome screen? Thirty minutes. The most difficult part was with the CANA kit, where to plug in the fan exactly; the instructions tell you which pins, but open the PI board instructions to see on a diagram where those two pins are.


Your Media: Hard Drives and Storage Options


There are many ways to get your media onto Plex. The easiest method is if you already have external drives; in that case, put your media on those and just plug them into the Pi. If you have a home server or a computer with samba you have shares on, you can link Plex to those.

But we'll assume you don't have either. This is all from scratch.

Here are your options:
1.) One or more external hard drives
2.) An external hard drive enclosure
3.) NAS

Let's break them down.

External Hard Drives

Examples:
WD 4T External Drive - $89.99
Seagate 2T External Drive - $59.99
WD 1T External Drive - $49.99

Pros:
This is a fantastic way to start your Plex life. It's easy and relatively cheap to suit your budget and the amount of media you have. Your server is a long term project; start small and easy and build up later. Try to get USB 3.0 if at all possible, however; you want the transfer speed.

Cons:
The Pi only has four USB slots, and only two are USB 3.0. You cannot have more than a max of 4 externals on the Pi and you won't want to use the 2.0 for anything 1080p or 4K, so effectively you can only attach to two USBs.

External Hard Drive Enclosures (and Internal Hard Drives)

Examples:
Yottamaster 2.5 Inch USB 3.0 External Hard Drive RAID Enclosure - $39.99
Yottamaster Aluminum 3.5" 2 Bay Type C Hard Drive Enclosure
Mediasonic ProBox HF2-SU3S2 4 Bay 3.5” SATA HDD Enclosure - $99.00
Mediasonic USB 3.1 4 Bay 3.5” SATA Hard Drive Enclosure - $139.99

Pros:
They hold many drives. Or 1 USB 3.0 = 1-12 separate drives.

The examples are both USB 3.0 or 3.1, so transfer is fast, and again, you're going to need that transfer speed. Some are for 2.5 drives, some for 3.5 drives, but you can get an adapter to put a 2.5 into a 3.5 bay if you have mixed sizes Some have a RAID option, so you can either RAID the drives or mount each drive separately in the PI so you can have a drive for each type of media. Hard drive enclosures come from 1 Bay to like, 12, but the most common and least expensive is 4 bay and below.

Cons:
Actual hard drives bought separately. Yeah.

However, drives are getting cheaper and again, we're just starting out. You can start off with just one drive. You can also use the enclosure to hold any old hard drives you may have lying around. The big advantage for using an enclosure like this over an external is the multiplication and ease of changing out drives. It's also cheaper in the long run. Internal drives go on sale much more often than externals.

Examples of Internal Hard Drives:
Seagate BarraCuda 2TB Internal Hard Drive HDD - $42.99
WL 3TB 7200RPM 64MB Cache SATA III 6.0Gb/s 3.5" Internal Desktop Hard Drive - $45.99
WL 4TB 7200RPM 64MB Cache SATA 6.0Gb/s (Enterprise Grade) 3.5" Hard Drive - $69.99
Seagate BarraCuda 8TB Internal Hard Drive HDD - $147.99

NAS

These could be considered one stop shopping for a Plex Media server, actually. They combine the hard drive enclosure--many drives--with a CPU and RAM; they are already configured and designed specifically for file storage and some you can install Plex on directly. Sometimes, they come with drives; somethings, you have to buy them separately. I don't know enough to have an option, as I don't use NAS, but everyone I know who does loves them for ease of use. There are tons of reviews on the best ones.

I will say for price point, they're not entry level. They're biggest disadvantage is the CPU is not powerful and you'll have to read carefully to make sure it can handle the media you want to play.


Pi Media Server: My Configuration


Now, time to assemble! This is an example but literally the one I put together this week

Pi Server Parts
1.) Raspberry PI with Cana Kit - $82.99
2.) Micro SD Card - 7.99 (I already had a SD card but it was about the same price)
3.) Mediasonic USB 3.1 4 Bay 3.5” SATA Hard Drive Enclosure - $139.99
4.) Drives - I already had about twelve in Watson Server, so the following were moved into it. I added price I paid at the time, but I buy drives whenever they go on sale and just keep them until they die.
a.) Adata Premiere 2.5 SSD, 250G - $63.49
b.) Hitach 3.5 SSD, 3T - $48.99
c.) Toshiba Enterprise 3.5, 3T - $63.89
d.) Seagate Barracuda 3.5, 8T (the one in the internal drive example above) - $149.99

Total for this configuration: $557.33
Total for me (pi and hard drive enclosure): $222.98 (via birthday, granted)
Total for internal drives: $326.36

This may look terrifyingly expensive, so let look at some alternate configurations. Remember, all you need for this is the Pi Kit, the microSD card, and some kind of media drive.


Pi Media Server: Alternate Configuration Examples For Your Budget


Let's look at other options.

Raspberry Pi: Required Parts
1.) Raspberry PI with Cana Kit - $82.99
2.) Micro SD Card - 7.99 (I already had a SD card but it was about the same price)
Total: $90.98

External Drive Options
With WD 1T External Drive - $49.99
Total with Pi and microSD Card: $140.97

With Seagate 2T External Drive - $59.99
Total with Pi and microSD Card: $150.97

With WD 4T External Drive - $89.99
Total with Pi and microSD Card: $180.97

Hard Drive Enclosure Options
With Yottamaster Aluminum Type C USB3.1 Gen2 10Gbps 3.5" Hard Drive RAID Enclosure 2 Bay for 3.5 Inch SATA HDD Support UASP - $69.99
Total with Pi and microSD Card: $160.97

Add: Seagate BarraCuda 2TB Internal Hard Drive HDD - $42.99
Total with Pi, microSD Card, and Enclosure: $203.96

Pi Plex Media Server: Software


Now, installing Plex: I read three tutorials to put this together. Here are the two most useful to me.

Links:
How to Setup a Raspberry Pi Plex Server
How to Turn a Raspbery Pi into a Plex Server

These will work fine, but I did run into some differences. At first, I was going to just write here my changes, but making you jump between if you don't want to may be confusing, so I'm reproducing the instructions they gave--or both if they differ--and adding my changes for Pi 4 and the latest OS update.

Note: This will all be in command line; don't be afraid. This will be very exact and I'll be telling you not only what to do but why and what it means. You cannot do this wrong, I promise; I'll be with you the entire time.

This will be in four parts.

Before You Install Plex


First, we have some things to do to get Pi ready to install Plex.

  1. Open a terminal window on your Pi desktop
    On your Raspberry Pi desktop, open a terminal window so we can get to the command line; it's up in the top bar to the left of center and looks like a small black screen. A window will open, all black, with 'pi@raspberry:' followed by an impatiently blinking cursor like it's really annoyed you're so slow. You get used to it.

  2. Make sure the Raspberry Pi OS is up to date.
    We need to check and makes sure all packages on the Pi are up to date.

    Run the following two commands:
    sudo apt-get update
    sudo apt-get upgrade


    What does this mean?
    'sudo' - 'substitute user do'. This is because you are right now logged in as 'pi', the default, not root. No, don't switch to root, please God. You don't need it and outside very specific circumstances don't want to because you can accidentally delete your entire operating system. I've done that. I've done terrible things with root. Please stay away from root if at all possible. We use 'sudo' instead for commands that only root has permission to do so we don't do that.

    'apt' - 'advanced packaging tool'. It retrieve the information and packages from the authenticated sources for installation, upgrade and removal of packages along with their dependencies. AKa this is the command to install, upgrade, and remove programs.

    'get' means get. So 'get advanced packaging tool'. You can now just use 'apt' but if you've been doing linux command line for a while, your fingers refuse to do that yet.

    'update' - the command to update sources, either 'sources.list', the list of sources for packages, or the list of sources in the directory /sources.list.d. For 'apt-get' to install or update anything, the location of that package must be in that list or in that folder. Otherwise, you have to use alternate means.

    'upgrade' - the command to update your OS. Yes, this confused the fuck out of me with 'update' for a while.


    Literally:
    'sudo apt-get update' - get apt to update sources.list or the sources in /sources.list.d
    'sudo apt-get upgrade' - get apt to update my OS and all dependencies.


    Voila! You now know a large part of basic Ubuntu. And that 'update' command is about to be super useful very soon.

  3. Install program to get Plex packages over https
    Most programs you download in Ubuntu are from one of the ubuntu-esque repositories ; that's what makes Ubuntu great. They are always compatible, they have all their pieces, no worries about anything. You just install them from there. However. Sometimes, the program isn't there, or a version you need isn't there. What do?

    For Raspberry Pi, Plex is not in the Ubuntu repository for its specific OS, so it needs to be acquired from Plex's only repository. That means we have to get the program from Plex itself, and find a way to make sure we can update it regularly after using that same repository.

    First step: we need the “apt-transport-https” package. This package allows 'apt' to retrieve packages over web, specifically from websites using 'https', which Plex uses. So we're going to install it using some of same commands as before.

    Run the following command:
    sudo apt-get install apt-transport-https


    What does this mean?
    'install' - install [a package]
    'apt-transport-https' - the name of the package/program


    Literally:
    'sudo apt-get install apt-transport-https' - get apt to install the package apt-transport-https


    Congratulations! You have just manually installed your first program on Ubuntu in command line!

  4. Add the key to the Plex repository to Pi
    As stated in one, most programs/packages exist in the Ubuntu repository and therefore the address is already on the source.list. However, some aren't, and to use apt to install them, the address must be added to sources.list or added to the sources.list.d directory. To do that, we first add a key. The key is used to ensure the files that you are downloading are in fact from that repository and signed by that key aka not malware or bad files.

    Run the following command to download and add the key to the package manager:


    What does this mean?
    This is going to need a more detailed breakdown as this is Advanced Beginner Linux, sort of. This is actually two separate commands, but the second depends on the first.

    First half, before '|':
    'curl' - this is a command used to transfer data/packages from a url (c + url!)
    'https://downloads.plex.tv/plex-keys/PlexSign.key' - url location of the key


    Literally:
    'curl https://downloads.plex.tv/plex-keys/PlexSign.key' - transfer this key over to me (Pi). We don't need 'sudo' here because 'curl' is not a command restricted only to root.


    Second half after '|':
    'apt-key' - this is a utility. It is used to manage the apt key repository. This is how the OS knows a package we want to install is trusted; it came from somewhere safe.
    'add -' - add [a key]


    Literally:
    'sudo apt-key add -' - add key to apt

    Now the middle part:
    '|' - this is a delimiter that you use to run two commands one after the other, but the second command runs using the output of the first one

    In this case, it takes the output of 'curl https://downloads.plex.tv/plex-keys/PlexSign.key', which is the key itself (PlexSign.key) and adds the key to the apt repository.


    Literally, All Together:
    'curl https://downloads.plex.tv/plex-keys/PlexSign.key | sudo apt-key add -' - transfer the key from there to here and add it to apt key repository


    Well done! Key is added!

  5. Add the Plex repository to Pi
    Finally! We are now going to add the Plex repository to our Pi so apt can install it.

    Run the following command:
    echo deb https://downloads.plex.tv/repo/deb public main | sudo tee /etc/apt/sources.list.d/plexmediaserver.list


    What does this mean?
    Two commands again, and as we know now, the second command is using the output on the first. Let's break it down.

    First half:
    'echo' - this command passes text strings as argument.

    You're going to see it in action right now. In your terminal, type the following and hit enter:
    echo 'frog'


    You should see 'frog' now in the next line. Congratulations! You know now about 10% of basic Ubuntu at this point.

    'deb https://downloads.plex.tv/repo/deb public main' - this is the literal string text but without the ' ' or " ". Just go with it. You do usually need that but not right now.


    Literally:
    'echo deb https://downloads.plex.tv/repo/deb public main' - take this line of text 'deb https://downloads.plex.tv/repo/deb public main' and display it.


    However, we do not want to display random text in the terminal; we want to do something else with it. That's what the second command is for.


    Second half:
    'tee' - this command reads input and writes it to output (terminal) and to one or more files.

    '/etc/apt/sources.list.d/plexmediaserver.list' - this is a file where apparently, something will be written

    Now, breaking it down:
    /etc/apt/sources.list/d/ are the directories; plexmediaserver.list is a file

    The /etc directory is where a lot of configuration files are stored, along with other things. The command 'apt' we know; /apt is the subdirectory (in /etc) where its files are located and where sources.list, the list of all the sources that apt uses to access to install and update packages, is located. The subdirectory /sources.list.d (in /etc/apt), is where sources are each individually listed in [name].list files.


    Literally:
    'sudo tee /etc/apt/sources.list.d/plexmediaserver.list' - read [something] and in the directory /etc/apt/sources.list.d create and write [something] into the file plexmediaserver.list or create that file and then write [something] into it.


    So we know '|' is the delimiter that tells us to use whatever comes out of the first command in the second one. I think we got this one

    Literally, All Together:
    'echo deb https://downloads.plex.tv/repo/deb public main | sudo tee /etc/apt/sources.list.d/plexmediaserver.list' - take 'deb https://downloads.plex.tv/repo/deb public main' and write it into the file 'plexmediaserver.list' (or create that file and write it into it) which is located in /etc/apt/sources.list.d/

    If you want to see it, you can!

    Run the following commands:
    cd /etc/apt/sources.list.d
    sudo nano plexmediaserver.list


    What does this mean?
    'cd' - change directory. Basically 'go to [this directory]'

    'nano' - this is a text program.


    Well done! To get out without doing anything, click ctrl-X to exit nano, then type the following command:
    cd /


    What does this mean?
    'cd /' - change directory to top level directory



  6. Use 'update' to add repository to OS
    You know, so apt and the OS know what we just did.

    Run the following command:
    sudo apt-get update


    Now, we're ready to install Plex!



Install Plex Media Server



  1. Install Plex Media Server

    Run the following command:
    sudo apt-get install plexmediaserver


    It may need dependencies, permissions, etc. No matter what it asks you, type 'y'. Then wait for it to finish.




Configure Plex Media Server Run Options


This is going to make your Plex life less complicated.

  1. Make sure Plex isn't running.

    Run the following command:
    sudo systemctl stop plexmediaserver


    What does this mean?
    'systemctl' - this is the init and system manager in ubuntu right now. It runs and manages and controls packages.

    'stop' - command telling systemctl to stop something


    Literally:
    'sudo systemctl stop plexmediaserver' - systemctl stop plexmediaserver from running


  2. Change default user in Plex
    This is just to save you time and headaches later. We're going to change the default user from 'plex' and default group 'plex' to 'pi' for both. There are many ways to do this, but my research states the best way to do this is using an override file so it won't get overwritten every tie you update.

    Run the following command:
    sudo systemctl edit plexmediaserver


    What does this mean?
    'edit' - tells systemctl to edit plexmedia server.

    What we're doing is creating an override file that will be located in /etc/systemd/system/plexmediaserver.service.d

    Now, it's blank, yes. You're creating right now.


    Write the following in the window:
    # override file for Plex
    # created by [your name or user]
    # [date]

    [Service]
    User=pi
    Group=pi


    Click Ctrl-S to save and Ctrl-X to exit.

    What does this mean?
    '#' - this comments out so its not read. Explanation of what file is, notes. Anytime you make or edit a file, add some notes on what you did and when. You will save yourself so much stress.

    '[Service]' - this corresponds to the section of the file we want to override. This file is located in /usr/lib/systemd/system/plexmediaserver.service (though mine is in /lib/systemd/system/plexmediaserver.service and I have no idea why).

    User and Group are self explanatory: we're saying the default user and group for plex is now pi.


  3. Set an IP for Pi (optional)
    You should do this but there are considerations. If you do, you absolutely have to to go into your router and reserve the address there for Pi. Otherwise, something else could take that IP and your Pi won't be able to access the internet.

    Now, I strongly recommend it because that way, when you want to access your server, you don’t have to look up the IP address each time. You can use 'localhost' to access it locally, however, without the specific ip.

    If you decide to do it--and again, I recommend it--there are two options.'m giving both, but I used the second one and didnt try the first.

    Option 1:


    1. Get your current ip address on the pi.

      Run the following command:
      hostname -I


      You should see the Pi's current IP address.

      What does it mean?
      'hostname' - command to get host info

      ' -I' - option to get only ip address

      Remember that, write it down, and/or highlight it with your mouse and click ctrl-C. Go ahead and write it down though, just in case. Now.


    2. Open cmdline.txt

      This will assign the ip at boot.

      Run the following command:
      sudo nano /boot/cmdline.txt


    3. Add IP to file

      At the bottom of the file, type the following:
      ip=[ip address]


      Click ctrl-S and then ctrl-X to exit.




    Option 2
    Gotten from here: How do I set up networking….


    1. Get your current IP

      Run the following command:
      ip -4 addr show | grep global


      You'll see something like this:
      inet 10.1.1.30/24 brd 10.1.1.255 scope global eth0
      inet 10.1.1.31/24 brd 10.1.1.255 scope global wlan0


      What does it mean?
      In the first line is the current IP of your Pi; I bolded it. The /24 at the end, though, isn't part of the IP address itself; it's the size of your network. Write down all of it; you need that /24 (or whatever your number is there).

      Your IP will probably be something like 192.168.1.[number] or 192.168.0.[number].

      The second line is network broadcast; don’t worry about it.


    2. Find the ip address of your router.

      Run the following command:
      ip route | grep default | awk '{print $3}'


      You'll see something like this:
      10.1.1.1


      Yours will probably be 192.168.1.1 or 192.168.0.1. It will definitely end with a '1' however.

      Write this down now.


    3. Find the dns server.

      Run the following command:
      cat /etc/resolv.conf


      You'll see something like this:
      nameserver 10.1.1.1


      What does it mean?
      This is your DNS nameserver. Usually, it will be the same as your router IP.


      Write this down now.


    4. Edit the dhcpd.conf file

      Run the following command:
      sudo nano /etc/dhcpd.conf


      This file may be blank; that's fine. You're creating it now.

      At the top, type this:
      # created by [your name or user]
      # [today's date]


      If your pi is on Ethernet/Lan, add this:
      interface eth0


      If your pi is on Wifi, add this:
      interface wlan0


      Then add this below it:
      static ip_address=[Your IP]/[Your Network Size Number]
      static routers=[Router IP]
      static domain_name_servers=[DNS IP]


      Now click Ctrl-S and then Ctrl-X to save and exit.


    5. Go to your router and reserve the address (without the /24).



  4. Attach your external hard drive(s) to Pi's USB 3.0 port(s) and reboot.


  5. Go to your Plex webpage

    On your Pi desktop or on your own computer, open a web browser and navigate to the following address:
    http://localhost:32400/web/

    Or:

    http://[Pi IP]:32400/web/

    And follow the instructions there to finish set up.



You did it! Well done!

Troubleshooting
If it's not running after reboot, don't worry; this happens about half the time after a Plex install.

Run the following commands:
sudo systemctl enable plexmediaserver
sudo systemctl start plexmediaserver


What does this mean?
'enable' - this tells systemclt to let Plex run at start up.

'start' - this tells systemclt to start Plex now.



Creating a Plex Bash Script


You may at times need command line to do things with plex, but one basic one is to start it, stop it, or check its status. This is a hassle to remember. So we'll write a script to help us out and you will write your very first bash script.

  1. Go to directory for scripts

    Run the following:
    cd /usr/local/bin


    This takes you to the default location for user scripts, /usr/bin.


  2. Create the script
    You can name it anything: mine is called myplex. I'm going to use my personal script as an example. So anywhere you see 'myplex', substitute whatever name you want that’s short and easy to remember and I mean both of those.

    Run the following:
    sudo nano myplex


    This will open the nano text editor with that name as default.

    Write the following in the window; I'll break it down for you after.
    #!/bin/bash

    # start/restart/stop/status plex
    # options start,stop,restart,status

    # variables
    x=$1

    # run
    sudo systemctl $x plexmediaserver

    # end
    exit 0


    Click Ctrl-S, then Ctrl-X to save and exit.

    What does it mean?
    Starting with the first line:

    '#!/bin/bash' - this declares the language we're using and where it's located. We're using bash, and bin is where it is located.

    '# start/restart/stop/status plex' - this is my notes, telling me what this script does. I'm telling myself this starts, restarts, stops, or checks the status of plex

    '# options start,stop,restart,status' - this is my notes, telling me the options I have for this script when it runs: start, stop, restart, or get status.

    '# variables' - this is my note telling me that here are my declared variables.

    'x=$1' - 'x' is the only variable I'm declaring. Giving it the inital value of '$1', however, indicates that the variable 'x' will be populated by the user when I run the script. I'll come back to what this means.

    '# run' - this is my note telling me where the script commands start. Basically, where the thing is done.

    'sudo systemctl $x plexmediaserver' - this looks a lot like the command we used to stop and start plexserver, but instead of 'stop' or 'start', there's a '$x'. The variable mentioned above.

    I said that the '$1' meant that the value is populated by the user at runtime, you can guess that '$x' is whatever the user populates it with. However, for this to work, it has to be a command that systemctl recognizes in relation to the program plexmediaserver. There are four I use for this script: start, restart, stop, status. Which you saw above that I wrote in my options in case I forgot, because I will. These four are the only values that the variable 'x' be populated with by the user.

    '# end' - my note saying this is the end of the script.

    'end 0' - end script


    Now, how do you use it? I'll tell you, but first, we have to make it work.

  3. View script directory>
    Now, let's look at our script in the directory.

    Run this command:
    ls -la


    You should see 'myplex' in the directory.

    What does this mean?
    'ls' - shows everything in that folder.

    '-la' - this two separate options for ls. '-l' puts everything in list format; '-a' means to show everything in the folder and everything about the file. You can combine them into a single '-la'


  4. Set ownership and permissions of script.

    Now, your file has some interesting info, probably something like this:
    -rw-r--r-- 1 root root 14 Feb 15 16:16 myplex


    What does this mean?
    We'll break it up using the spaces; these are columns showing different info about your file.

    From left to right:
    '-rw-r--r--' - we're going to need to break this down even more. There are ten characters in this and the first is separate from the other nine
    .

    First character - this indicates type in the directory. Examples: a directory is 'd'; a link is 'l'; a file is '-'.

    Your File:
    '-' - this means that myplex is a file



    Second through tenth characters - these indicate the permissions this file has.

    'rw-r--r--' - this represents all current permissions. Specifically, three groups of permissions: user, group, and other; in each of these, there are three types of permission: read, write, execute. So, the first three characters are user/file owner permissions; the second three are group permissions, and the third three are other permissions.

    Permissions:
    'r' - read permission. If you don't have read permission, this appears as '-'
    'w' - write permission. If you don't have write permission, this appears as '-'
    'x' - execute permission. if you don't have execute permission, this appears as '-'

    Now, let's look at what we have for this file. We'll split it up into the three groups: user, group, and other

    Group one: user
    'rw-' - user or file owner permissions. 'rw-' indicates they have read and write permission but do NOT have execute permissions. That '-' means no permission; if they did have execute permission, it would have an 'x'

    Group two: group
    'r--' - group permissions. 'r--' indicates they have read permissions but not write or execute permissions

    Group three: other
    'r--' - other permissions. 'r--' indicates they have read permissions but not write or execute permissions

    So while the owner--whoever it is--has read and write, aka they can look at the file and edit it, they can't execute. No one else can even edit; they can only read it.


    Now the rest of that line:
    '1' -not important

    'root' - this is the owner of the file.

    'root' - this indicates the group the file belongs to

    '14' - this indicates the size of the file

    'Feb 15' - date file was created or last accessed

    '16:16' - time file was created or last accessed

    'myplex' - file name


    Now, what we're going to do is change the ownership to you--pi--and fix the permissions so we can execute it.

    Run the following commands:
    sudo chmod 777 myplex
    sudo chown nobody.nogroup myplex


    What does this mean?
    'chmod' - this is the command that changes the access permissions of the file.

    '777' - this is a shortcut for doing all the permissions at once. '7' is the equivalent of 'rwx'. Having three of them changes the permissions of user, group, and global to 'rwx' all at once. It's fast, is what I’m saying.

    'chown' - this is the command to change the user (owner) and group of the file

    'nobody:nogroup' - this indicates the user:group

    'nobody' - this means no one owns it so everyone owns it

    'nogroup' - this is an actual group name. Ironically it means anyone in any group can run it.

    Doing this will make this script able to be read it, written to, or executed by anyone.


    Now run this command again:
    ls -la


    You should see this:
    -rwxrwxrwx 1 nobody nogroup 14 Feb 15 16:16 myplex


    Now it is executable and anyone can read it, write/edit it, and run it.


  5. Run the script
    The moment you were waiting for.

    Run the following command:
    myplex status


    It may ask for your password. This is normal.

    You should see a whole bunch of info about your plex server aka its current status. Cool.

    What does it mean?
    'myplex' - your script name

    'status' - the user input that populates x=$1.


    Now you know why we had that x=$1; $1 indicates that the script itself takes an argument from the user on the command line when you run it. So 'myplex stop' is the equivalent of 'sudo systemctl stop plexmediaserver'

    You can myplex stop, myplex start, myplex restart, and myplex status.

    Congratulations! You have now run your first bash script.




Now, enjoy your new media server.

ETA: Edited for readability.

Posted at Dreamwidth: https://seperis.dreamwidth.org/1070790.html. | You can reply here or there. | comment count unavailable comments
Tags: coding, how to plex, my relationship with electronics, plex, raspberry pi
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments