ตั้งให้ Firefox ปิดแท็บที่เปิดครั้งที่แล้ว (session เก่า) ก่อนรันแท็บใหม่ทั้งหมดให้รันอัตโนมัติหลัง login (Autostart) CentOS9


ต้องสร้างไฟล์ .desktop ในโฟลเดอร์ autostart ของ user:

~/.config/autostart/firefox_cctv.desktop

โดยให้ Exec ชี้ไปที่สคริปต์:

[Desktop Entry]
Type=Application
Exec=/home/mill/start_firefox_cctv.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Firefox CCTV Home88
Comment=Start Firefox with CCTV page (clear old tabs first)

กรณีอยากใช้ได้ทุก user

วางสคริปต์ไว้ที่ /usr/local/bin/ (เหมาะกับไฟล์ custom script ที่ต้องใช้ system-wide):

/usr/local/bin/start_firefox_cctv.sh

แล้วทำให้รันได้:

sudo chmod +x /usr/local/bin/start_firefox_cctv.sh

จากนั้นทุก user จะสามารถพิมพ์:

start_firefox_cctv.sh

ได้เลย (เพราะ /usr/local/bin อยู่ใน $PATH แล้ว)

โค้ดใน start_firefox_cctv.sh

#!/bin/bash

# ====== หาโฟลเดอร์โปรไฟล์ Firefox ======
PROFILE_DIR=$(grep -E '^Path=' ~/.mozilla/firefox/profiles.ini | head -n 1 | cut -d= -f2)

# ====== ลบไฟล์ session เก่า ======
rm -f ~/.mozilla/firefox/$PROFILE_DIR/sessionstore.jsonlz4
rm -f ~/.mozilla/firefox/$PROFILE_DIR/sessionCheckpoints.json
rm -f ~/.mozilla/firefox/$PROFILE_DIR/recovery.jsonlz4
rm -f ~/.mozilla/firefox/$PROFILE_DIR/recovery.baklz4

# ====== เปิด Firefox แบบไม่ใช้ session เดิม ======
nohup firefox --new-instance --no-remote \
    http://test.com/1.html \
    http://test.com/2.html \
    http://test.com/3.html \
    >/dev/null 2>&1 &

อธิบายโค้ดสคริปต์นี้ทีละบรรทัด

#!/bin/bash

บอกระบบว่าไฟล์นี้เป็น สคริปต์ bash

# ====== หาโฟลเดอร์โปรไฟล์ Firefox ======
PROFILE_DIR=$(grep -E '^Path=' ~/.mozilla/firefox/profiles.ini | head -n 1 | cut -d= -f2)
  • Firefox จะเก็บ session/tab เก่าไว้ใน โฟลเดอร์โปรไฟล์
  • คำสั่งนี้ใช้ grep หาแถวที่ขึ้นต้นด้วย Path= ในไฟล์ profiles.ini (ซึ่งบอกตำแหน่งโปรไฟล์)
  • head -n 1 → เอาเฉพาะบรรทัดแรก (กรณีมีหลายโปรไฟล์)
  • cut -d= -f2 → ตัดเอาค่า path หลังเครื่องหมาย =
  • ผลลัพธ์เก็บในตัวแปร $PROFILE_DIR เช่น abcd1234.default-release
# ====== ลบไฟล์ session เก่า ======
rm -f ~/.mozilla/firefox/$PROFILE_DIR/sessionstore.jsonlz4
rm -f ~/.mozilla/firefox/$PROFILE_DIR/sessionCheckpoints.json
rm -f ~/.mozilla/firefox/$PROFILE_DIR/recovery.jsonlz4
rm -f ~/.mozilla/firefox/$PROFILE_DIR/recovery.baklz4

Firefox จะจำแท็บที่เปิดล่าสุดไว้ในไฟล์พวกนี้

  • sessionstore.jsonlz4 → เก็บแท็บที่เปิดล่าสุด
  • sessionCheckpoints.json → เก็บ checkpoint ของ session
  • recovery.jsonlz4 และ recovery.baklz4 → เก็บข้อมูลกู้คืน session เผื่อโปรแกรม crash

การลบไฟล์เหล่านี้ทิ้ง = เคลียร์ประวัติแท็บที่เปิดไว้ → ทำให้ Firefox เริ่มแบบ “หน้าใหม่”

# ====== เปิด Firefox แบบไม่ใช้ session เดิม ======
nohup firefox --new-instance --no-remote \
    http://test.com/1.html \
    http://test.com/2.html \
    http://test.com/3.html \
    >/dev/null 2>&1 &
  • nohup → รันโปรแกรมโดยไม่ผูกกับ terminal (ไม่ถูก kill ตอน logout/autostart)
  • firefox --new-instance --no-remote
    • --new-instance → บังคับเปิด Firefox ใหม่ แม้มี instance เดิมอยู่
    • --no-remote → ป้องกันไม่ให้ไป reuse session เดิม
  • ด้านหลังเป็น URL ที่ต้องการเปิดในแท็บใหม่
  • >/dev/null 2>&1 → ซ่อนข้อความที่ Firefox ส่งออกมา (ทั้ง stdout และ stderr)
  • & → รัน Firefox เป็น background process ทำให้สคริปต์ไม่ต้องรอ

สรุป:

  1. สคริปต์หาตำแหน่งโฟลเดอร์โปรไฟล์ Firefox
  2. ลบไฟล์ session เดิมออก เพื่อไม่ให้เปิดแท็บเก่า
  3. สั่ง Firefox เปิดใหม่ด้วย URL ที่ต้องการ โดยรันแบบ background ไม่ถูก kill ทิ้ง

, ,