meganz / MEGAcmd

Command Line Interactive and Scriptable Application to access MEGA
Other
1.9k stars 407 forks source link

MEGAcmd - run mega-cmd-server, if not already running #499

Open zDEFz opened 3 years ago

zDEFz commented 3 years ago

Hello dear community,

Ubuntu 18.04 LTS user here.

Mega-cmd-server (sync) stops randomly after some time. Abusing mega-put each night 4am to initiate the server. That is not a nice solution, as it hammers my email full.

I want to put a script in the crontab each 5 minutes, that checks if the mega-cmd-server is started, and if not, start it.

How to do that?

Thanks for your support.

xeysz commented 3 years ago

I created this systemd user service to keep mega-cmd-server running.

Create file $HOME/.config/systemd/user/mega-cmd.service

[Unit]
Description=MEGA cmd server
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/mega-cmd-server
Restart=always

[Install]
WantedBy=default.target

Save the file and run

systemctl --user --daemon-reload
systemctl --user enable mega-cmd.service
systemctl --user start mega-cmd.service
systemctl --user status mega-cmd.service

Use exit --shell-only when in the mega-cmd shell to keep the server running

weiqi-chen commented 2 years ago

I created this systemd user service to keep mega-cmd-server running.

Create file $HOME/.config/systemd/user/mega-cmd.service

[Unit]
Description=MEGA cmd server
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/mega-cmd-server
Restart=always

[Install]
WantedBy=default.target

Save the file and run

systemctl --user --daemon-reload
systemctl --user enable mega-cmd.service
systemctl --user start mega-cmd.service
systemctl --user status mega-cmd.service

Use exit --shell-only when in the mega-cmd shell to keep the server running

Rember to run sudo loginctl enable-linger <user> first if you doing it in a raspberry pi .

adyomin commented 2 years ago

systemctl --user --daemon-reload

Did not work with

$ systemctl --version  
systemd 248 (v248.7-1.fc34)
+PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified

But systemctl --user daemon-reload.

wapfalls commented 2 years ago

For me, even systemd was not reliable, but found pm2 to keep the server running,

sudo apt install npm
sudo npm install pm2@latest -g
pm2 start /usr/bin/mega-cmd-server --name=mega_cmd_server
pm2 ls

You may follow the steps here to auto start the MEGAcmd server using pm2 on reboots: https://pm2.keymetrics.io/docs/usage/startup/

Also the right command to exit interactive shell without stopping the server is, exit --only-shell

Farossco commented 1 year ago

If you have root access, what worked well for me was to create a mega-server@.service in /usr/lib/systemd/system/:

[Unit]
Description=MEGA cmd server service for user %I
Wants=network-online.target
After=local-fs.target network-online.target nss-lookup.target

[Service]
Type=simple
User=%i
ExecStart=/usr/bin/mega-cmd-server
Restart=always

[Install]
WantedBy=multi-user.target

Then, run as root:

systemctl --daemon-reload

And the following (still as root) for each user needing the mega-cmd-server in the background: (replace '[user_name]' with the proper user name)

systemctl enable mega-cmd@[user_name].service
systemctl start mega-cmd@[user_name].service
systemctl status mega-cmd@[user_name].service

If you get Another instance of MEGAcmd Server is running. Execute with --skip-lock-check to force running (NOT RECOMMENDED) in the status, stop the corresponding instance by running the following as the user:

mega-cmd
exit

then systemctl restart mega-cmd@[user_name].service as root

Hope this helps 🙂

xlad commented 10 months ago

If you have root access, what worked well for me was to create a mega-server@.service in /usr/lib/systemd/system/:

mega-cmd@.service

or use mega-server@.service everywhere where mega-cmd@.service is used

Also on Ubuntu is good to make a symlink in /etc

ln -s /usr/lib/systemd/system/mega-server@.service /etc/systemd/system/mega-server@.service