Yes, you can create a script that executes the specified block of commands with a single command and ensure it remains available and functional even after your Ubuntu server restarts. Below is a comprehensive guide to achieving this:

1. **Create the Shell Script**
2. **Make the Script Executable**
3. **Place the Script in a Directory Included in `PATH`**
4. **Ensure the Script Persists After Reboots**
5. **(Optional) Automate Execution on Server Startup**

---

### 1. **Create the Shell Script**

First, create a shell script that contains your command block. For this example, we'll name the script `deploy.sh`.

**Steps:**

1. **Navigate to a Suitable Directory**

   It's common to store custom scripts in `/usr/local/bin` or your home directory. For this example, we'll use `/usr/local/bin`.

   ```bash
   cd /usr/local/bin
   ```

2. **Create the `deploy.sh` Script**

   Use a text editor like `nano` to create the script.

   ```bash
   sudo nano deploy.sh
   ```

3. **Add the Following Content to the Script**

   ```bash
   #!/bin/bash

   # Exit immediately if a command exits with a non-zero status
   set -e

   # Navigate to your project directory
   cd /path/to/your/laravel/project

   # Pull the latest code as www-data user
   sudo -u www-data git pull

   # Restart Laravel queue workers
   php artisan queue:restart

   # Clear all caches
   php artisan cache:clear-all

   # Build frontend assets
   npm run build

   echo "Deployment tasks completed successfully."
   ```

   **Notes:**
   
   - **`set -e`**: Ensures the script exits immediately if any command fails, preventing unintended consequences.
   - **`cd /path/to/your/laravel/project`**: Replace `/path/to/your/laravel/project` with the actual path to your Laravel project.
   - **`sudo -u www-data git pull`**: Runs `git pull` as the `www-data` user. Ensure that the `www-data` user has the necessary permissions for the Git repository.
   - **`php artisan cache:clear-all`**: If `cache:clear-all` is a custom Artisan command, ensure it's correctly implemented. The default Laravel command is `php artisan cache:clear`.
   - **`npm run build`**: Assumes that `npm` is installed and properly configured in your environment.

4. **Save and Exit**

   If you're using `nano`, press `CTRL + O` to write the changes and `CTRL + X` to exit.

---

### 2. **Make the Script Executable**

Change the script's permissions to make it executable.

```bash
sudo chmod +x /usr/local/bin/deploy.sh
```

---

### 3. **Place the Script in a Directory Included in `PATH`**

By placing the script in `/usr/local/bin`, it's already in the system's `PATH`, allowing you to execute it from anywhere using `deploy.sh`.

**Verify it's in `PATH`:**

```bash
echo $PATH
```

You should see `/usr/local/bin` listed. If not, you can choose a directory that is in your `PATH` or add `/usr/local/bin` to your `PATH`.

---

### 4. **Ensure the Script Persists After Reboots**

Since the script is stored in `/usr/local/bin`, it will persist across server restarts. However, if your script relies on certain services or environment variables, ensure those are also persistent and correctly configured.

**Additional Considerations:**

- **Environment Variables:** If your script relies on specific environment variables, consider sourcing them within the script or ensuring they're globally available.
  
- **Absolute Paths:** Use absolute paths for all commands and file references within the script to avoid issues related to relative paths.

---

### 5. **(Optional) Automate Execution on Server Startup**

If you want the script to run automatically every time the server restarts, you can set it up using `systemd` or `cron`. Below are methods for both.

#### **A. Using `systemd`**

1. **Create a systemd Service File**

   ```bash
   sudo nano /etc/systemd/system/deploy.service
   ```

2. **Add the Following Content**

   ```ini
   [Unit]
   Description=Laravel Deployment Script
   After=network.target

   [Service]
   Type=oneshot
   ExecStart=/usr/local/bin/deploy.sh
   User=your-username
   Group=www-data
   Environment=PATH=/usr/bin:/usr/local/bin
   WorkingDirectory=/path/to/your/laravel/project

   [Install]
   WantedBy=multi-user.target
   ```

   **Notes:**
   
   - **`User`**: Replace `your-username` with the user that should execute the script.
   - **`WorkingDirectory`**: Ensure this matches the `cd` command in your script.
   - **`Environment`**: Adjust the `PATH` if necessary.

3. **Reload systemd and Enable the Service**

   ```bash
   sudo systemctl daemon-reload
   sudo systemctl enable deploy.service
   ```

4. **Start the Service**

   ```bash
   sudo systemctl start deploy.service
   ```

5. **Verify the Service Status**

   ```bash
   sudo systemctl status deploy.service
   ```

   This will show whether the script ran successfully.

#### **B. Using `cron` with `@reboot`**

1. **Edit the Crontab**

   ```bash
   crontab -e
   ```

2. **Add the Following Line**

   ```bash
   @reboot /usr/local/bin/deploy.sh >> /var/log/deploy.log 2>&1
   ```

   **Notes:**
   
   - **Logging:** This directs both standard output and errors to `/var/log/deploy.log`. Ensure this file is writable or choose an appropriate path.
   - **Environment:** `cron` runs with a limited environment. If your script relies on specific environment variables, set them within the script.

3. **Save and Exit**

   The script will now run automatically every time the server reboots.

---

### **Usage**

After completing the above steps, you can execute your deployment script with a single command from anywhere in your terminal:

```bash
deploy.sh
```

**Or, if you prefer:**

If you named the script differently or placed it elsewhere, adjust the command accordingly, e.g., `/usr/local/bin/deploy.sh`.

---

### **Best Practices and Additional Tips**

1. **Error Handling:**

   - Incorporate error handling within your script to manage failures gracefully.
   - Example:

     ```bash
     #!/bin/bash
     set -e

     cd /path/to/your/laravel/project

     sudo -u www-data git pull || { echo "Git pull failed"; exit 1; }
     php artisan queue:restart || { echo "Queue restart failed"; exit 1; }
     php artisan cache:clear-all || { echo "Cache clear failed"; exit 1; }
     npm run build || { echo "NPM build failed"; exit 1; }

     echo "Deployment tasks completed successfully."
     ```

2. **Logging:**

   - Implement logging to track the execution of your script, especially if automating it.
   - Example:

     ```bash
     #!/bin/bash
     set -e

     LOG_FILE="/var/log/deploy.log"

     {
         echo "Deployment started at $(date)"
         cd /path/to/your/laravel/project
         sudo -u www-data git pull
         php artisan queue:restart
         php artisan cache:clear-all
         npm run build
         echo "Deployment completed successfully at $(date)"
     } >> "$LOG_FILE" 2>&1
     ```

3. **Security Considerations:**

   - Ensure that only authorized users can execute the script, especially since it uses `sudo`.
   - Limit permissions on the script:

     ```bash
     sudo chown root:root /usr/local/bin/deploy.sh
     sudo chmod 750 /usr/local/bin/deploy.sh
     ```

   - **Sudoers Configuration:**

     If you want to allow specific commands without a password, edit the sudoers file using `visudo`:

     ```bash
     sudo visudo
     ```

     Add a line like:

     ```bash
     your-username ALL=(www-data) NOPASSWD: /usr/bin/git
     ```

     **Caution:** Be careful when modifying the sudoers file to avoid security vulnerabilities.

4. **Environment Consistency:**

   - Ensure that the environment in which the script runs (especially when automated via `systemd` or `cron`) has access to all necessary environment variables and paths.

5. **Testing:**

   - Before automating, manually run the script to ensure it behaves as expected.

     ```bash
     sudo /usr/local/bin/deploy.sh
     ```

---

By following this guide, you'll have a robust, single-command deployment script that remains functional and accessible even after your Ubuntu server restarts. Additionally, by optionally automating its execution on startup, you can further streamline your deployment or maintenance processes as needed.