Autostart @open-wa/wa-automate on Raspberry Pi using pm2

@open-wa/wa-automate is a splendid open-source project to play with whatsapp. It supports docker and Easy API with its plug and play feature. However there are few limitations one may face, as follows;

  • Docker image is available for x86-64 architecture which will not work on our favorite Raspberry Pi.
  • Easy API does not have any builtin support for auto-restart on boot or failure.

One may build the docker image for Raspberry Pi but it needs some knowledge about how the docker works. Easy API can be made up its auto-start capabilities using pm2, however there are no instructions available on the internet regarding “Autostart @open-wa/wa-automate on Raspberry Pi using pm2”. There is already a plenty of information available over the internet regarding installing nodejs, npm. So we decided to write a quick note about auto start @open-wa/wa-automate using pm2.

The Procedure:

  • Update the Nodejs package along with npm to avoid any issues during the procedure.
curl -sSL https://deb.nodesource.com/setup_16.x | sudo bash -

sudo apt install -y nodejs
  • First of all, run npx @open-wa/wa-automate --d --ras-pi --useChrome --multiDevice  and scan the code to save the session, so that it does not require authentication in future.
  • Now install pm2 sudo npm install -g pm2.
  • You can check the status of apps running under pm2 by command pm2 status and it will display out all the running apps.
  • To auto start pm2 on boot, issue the command pm2 startup and follow the on the screen instructions. For example;
[email protected]:~ $ pm2 startup
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi
  • The command sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/picreates a systemd script which helps in auto rebooting the pm2 process.
[email protected]:~ $ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 st                                                                             artup systemd -u pi --hp /home/pi
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=pi
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/u                                                                             sr/local/games:/usr/games:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin                                                                             :/usr/bin
Environment=PM2_HOME=/home/pi/.pm2
PIDFile=/home/pi/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-pi.service
Command list
[ 'systemctl enable pm2-pi' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-pi.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-pi...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-pi.service → /et                                                                             c/systemd/system/pm2-pi.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd
  • pm2 does not work the flags used with @open-wa/wa-automate, so we have to create a startup script.
    • Navigate to the directory where @open-wa/wa-automate is installed cd /the/directory/where/app/is/installed.
    • Create a file using nano editor,nano start.sh and paste the following contents in it.
#!/bin/bash
cd /home/pi/where/@open-wa/wa-automate/is/installed
npx @open-wa/wa-automate --d --ras-pi --useChrome --multiDevice
    • Save the filed using Ctrl + X keys, followed by Enter key.
  • Run the script using the command pm2 start start.sh --restart-delay 20000.
  • Save the pm2 app pm2 save so that pm2 restart the @open-wa/wa-automate on boot.
  • Now @open-wa/wa-automate will auto-start on boot / failure, have fun!.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x