Posts in category “Tutorial”

How to Enable User-Level Systemd Services to Start Automatically on Ubuntu After Reboot

TL;DR

If your user-level systemd service on Ubuntu doesn't start automatically after a reboot, enable it with systemctl --user enable HappyNotes.Api.service. If you encounter an error about an existing symlink, remove it first. To allow the service to run without an active user session, enable lingering using loginctl enable-linger <username>. Finally, ensure your service file has the correct [Install] section and reboot to check if the service starts as expected.

The Problem

After configuring a user-level service with systemd, you might find that it remains inactive after rebooting your server. For example, you may run the command:

systemctl --user status HappyNotes.Api.service

And see output indicating that the service is inactive (dead) and disabled.

Solution Steps

  1. Enable the Service: First, ensure your service is enabled to start at boot:

    systemctl --user enable HappyNotes.Api.service
    

    If you encounter an error stating that the service is already linked, you may need to remove the existing symlink:

    rm ~/.config/systemd/user/default.target.wants/HappyNotes.Api.service
    
  2. Check for Linger: User-level services require an active user session to run. To allow your user services to run even when you're not logged in, enable lingering:

    loginctl enable-linger $USER
    
  3. Verify Service Configuration: Ensure your service file has the correct [Install] section:

    [Install]
    WantedBy=default.target
    
  4. Reboot and Test: After enabling lingering and ensuring your service is set up correctly, reboot your server:

    sudo reboot
    

    After rebooting, check the status of your service again:

    systemctl --user status HappyNotes.Api.service
    

By following these steps, you can ensure that your user-level systemd services start automatically after a reboot on Ubuntu. Enabling lingering is the key, which is particularly useful for server environments where continuous operation of services is desired.

Docker Volumes in Production: A Practical Guide to Named Volumes vs Bind Mounts

When working with Docker containers in production, understanding volume management is crucial. This guide will help you make informed decisions about when to use named volumes versus bind mounts (directory mapping).

TL;DR

  • Use named volumes for persistent data (databases, application state)
  • Use bind mounts for config files and development
  • Combine both in production for optimal setup

Understanding the Basics

Named Volumes

volumes:
  - postgres_data:/var/lib/postgresql/data

Docker manages these volumes internally. Think of them as "black boxes" that Docker handles for you.

Bind Mounts (Directory Mapping)

volumes:
  - ./config:/etc/app/config

You manage these directories directly on your host machine.

When to Use What?

Use Named Volumes For:

  1. Database storage
  2. Application state
  3. Generated assets
  4. Any data that needs persistence but not direct access

Benefits:

  • Managed by Docker
  • Better performance
  • Automatic permissions handling
  • Easier backups
  • Portable across environments
  • Built-in volume management commands

Use Bind Mounts For:

  1. Configuration files
  2. Static files during development
  3. Source code in development
  4. Any files you need to edit from host

Benefits:

  • Direct access from host
  • Easy to edit
  • Version control friendly
  • Quick updates without container restart
  • Shareable across environments

Real-World Example

Here's a typical production setup combining both approaches:

version: '3'
services:
  db:
    image: postgres:15
    volumes:
      # Data persistence with named volume
      - postgres_data:/var/lib/postgresql/data
      # Configuration with bind mounts
      - ./config/postgres.conf:/etc/postgresql/postgresql.conf:ro

  nginx:
    image: nginx
    volumes:
      # Config files with bind mounts
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./config/nginx/conf.d:/etc/nginx/conf.d:ro
      # Static files with bind mount
      - ./static:/usr/share/nginx/html:ro

  app:
    image: node:18
    volumes:
      # Application data with named volume
      - app_data:/app/data
      # Config with bind mount
      - ./config/app.json:/app/config/app.json:ro

volumes:
  postgres_data:
  app_data:

Migration Tips

Moving from Bind Mounts to Named Volumes

If you're currently using bind mounts for data and want to switch to named volumes:

# Create new volume
docker volume create myapp_data

# Copy data
docker run --rm \
  -v /old/path:/source:ro \
  -v myapp_data:/destination \
  ubuntu \
  bash -c "cp -av /source/. /destination/"

Best Practices

  1. Named Volumes

    • Always use for persistent data
    • Name them descriptively
    • Regular backups
    • Don't manipulate directly on host
  2. Bind Mounts

    • Use read-only (:ro) when possible
    • Keep configs in version control
    • Use relative paths for portability
    • Store in a config/ directory
  3. General

    • Document your volume strategy
    • Regular backups for both types
    • Monitor disk usage
    • Use clear naming conventions

Common Pitfalls to Avoid

  1. Using bind mounts for database storage
  2. Hardcoding absolute paths
  3. Not setting proper permissions
  4. Forgetting to backup named volumes
  5. Direct manipulation of named volume directories

Conclusion

The key to successful Docker volume management is using the right tool for the job:

  • Named volumes for data that needs persistence
  • Bind mounts for configs and development
  • Combine both for a robust production setup

Remember: When in doubt, prefer named volumes for data and bind mounts for configuration.

Step 6 in detail: Configure Telegram Sync in HappyNotes

If you haven't read Step1 to Step 5, click Step 1 ~ Step 5

  1. Open the HappyNotes App:

    • Open https://happynotes.shukebeta.com with your favorite broswer and then navigate to the **Settings** page.
  2. Access Telegram Sync Settings:

    • Within the Settings page, look for the "Note Sync - Telegram" option.
    • Tap on it to open the sync settings page.
  3. Add a New Sync Configuration:

    • On the sync settings page, click on the "+ Add" button to create a new sync configuration.
  4. Enter the Required Information:

    • Source Note Choose the appropriate source note to sync from the options provided (e.g., Public, Private, All, Tag).

      • All - for backup purpose. It will sync every new note to the channel you specified. (Please don't use a public channel if you have any private notes!!!)
      • Private - only sync private notes to specified channel, you definitely should not use a public channel for this type of notes!
      • Public - only sync public notes to specified channel - if you use a telegram channel for your blog or microblog purpose, this option is for you!
      • Tag - only sync notes that are tagged with specific tag, You must know that it does not check the private/public flag, every new note that has the specified tag will be sent to the specified channel.
    • Channel Id: In the "Channel Id" field, enter the Channel Id you obtained earlier which looks like -100123456789.

    • Channel Name: This is a remark for the channel id, you can use the same channel name on Telegram or anything else you want.

    • Telegram Bot Token: In this field, paste your Telegram Bot Token . I strongly recommend you to use copy/paste feature instead of manually typing the token to avoid typos.

    • **Token Remark: ** Just like the channel name, this Token Remark helps you remember which token you are using for this channel. You can use your bot's name or anything else you want.

  5. Save the Configuration:

    • After filling in the details, click the "Save" button to store your sync configuration.
  6. Test the Sync Setting:

    • Once saved, you'll see your new configuration listed on the sync settings page.
    • Tap the "Test" button to send a test message to your Telegram channel.
    • If the test is successful, you’ll receive a confirmation message in your Telegram channel indicating that the sync is working, and the Test button will disappear.
  7. Activate or Adjust Sync Settings:

    • You can Disable or Activate or Delete a setting based on your requirements.
    • Make sure your bot is active and configured correctly to ensure smooth synchronization.

How to Sync Your Telegram Channel with HappyNotes

Happy Notes app recentlys supports to sync new notes to your specified telegram channel based on the rules you set. If you want to enjoy seamless note synchronization between your Telegram channel and the HappyNotes app, follow these easy steps:

Step 0: Find and Add @getidsbot

  • Open Telegram and search for @getidsbot.
  • Start a chat with the bot by clicking on "Start."

Step 1: Create Your Telegram Bot

  • Search for @BotFather in Telegram and start a conversation.
  • Use the /newbot command to create a new bot.
  • Follow the prompts to give your bot a name and username.
  • Note: The bot's username must end with "bot" (e.g., MySyncBot).

Step 2: Get Your Telegram Bot Token

  • After creating the bot, @BotFather will provide you with a token. This token is essential for integrating your bot with other services, so keep it safe.

Step 3: Add Your Bot to Your Telegram Channel as an Admin

Convenient Method:

  1. Open the conversation with your newly created bot in Telegram.
  2. Tap on the bot's icon in the top right corner.
  3. Click on "Add to Group or Channel".
  4. Select the channel you want to add the bot to, and it will automatically be added as an admin with the necessary permissions.

Alternative Method:

  1. Go to your Telegram channel's settings.
  2. Select "Administrators" and manually add your newly created bot as an admin.
  3. Ensure the bot has the necessary permissions to send and manage messages.

Step 4: Manually Write the First Message to the Channel

  • Send a message to your channel as you usually would. This message is necessary to identify the channel ID later.

Step 5: Forward the Message to @getidsbot

  • Forward the message you just sent to @getidsbot.
  • The bot will reply with the channel ID. Keep this ID safe; you’ll need it for the next step.

Step 6: Configure Telegram Sync in HappyNotes

Step 7: Test the Setting

  • Use the "Test" option in HappyNotes to send a test message to your Telegram channel.
  • Ensure the message is successfully sent, confirming the integration is working.

Step 8: Enjoy Syncing Your Notes with Telegram!

  • With everything set up, your notes will now sync with your Telegram channel automatically.
  • You can now easily access your notes from within Telegram, making your workflow even smoother.