Skip to content

Instantly share code, notes, and snippets.

@felipealfonsog
Created September 4, 2024 05:32
Show Gist options
  • Save felipealfonsog/0dd54bb6bfc38387b7aad79d4cbd0190 to your computer and use it in GitHub Desktop.
Save felipealfonsog/0dd54bb6bfc38387b7aad79d4cbd0190 to your computer and use it in GitHub Desktop.
Manage onedrive on Arch Linux, using systemd to enable or disable a service or daemon for continuous syncing.
#!/bin/bash
SERVICE_NAME="onedrive"
CONFIG_DIR="$HOME/.config/onedrive"
SYNC_DIR="$HOME/OneDrive"
# Check if onedrive is installed
if ! command -v onedrive &> /dev/null; then
echo "'onedrive' command not found. Please install it with yay or check your installation."
exit 1
fi
# Function to create the systemd service file
create_service() {
echo "[Unit]
Description=OneDrive Free Client
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/onedrive --monitor --confdir=$CONFIG_DIR --syncdir=$SYNC_DIR
Restart=on-failure
RestartSec=3
[Install]
WantedBy=default.target" | sudo tee /etc/systemd/system/onedrive.service > /dev/null
echo "'onedrive' service created at /etc/systemd/system/onedrive.service."
}
# Function to start the service
start_service() {
sudo systemctl start $SERVICE_NAME
sudo systemctl enable $SERVICE_NAME
echo "'onedrive' service enabled and set to start on boot."
}
# Function to stop the service
stop_service() {
sudo systemctl stop $SERVICE_NAME
sudo systemctl disable $SERVICE_NAME
echo "'onedrive' service stopped and disabled from starting on boot."
}
# Function to perform a one-time sync
sync_once() {
onedrive --confdir="$CONFIG_DIR" --syncdir="$SYNC_DIR"
echo "Manual OneDrive sync completed."
}
# Display options menu
echo "Select an option:
1. Enable continuous OneDrive syncing
2. Disable continuous OneDrive syncing
3. Sync OneDrive once
4. Exit"
read -rp "Enter your choice (1-4): " OPTION
case $OPTION in
1)
create_service
start_service
;;
2)
stop_service
;;
3)
sync_once
;;
4)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment