Ubuntu – auto-run scripts after reboot

How to restart the script after startup.

Configuring autostart

We will use cron to autorun the script we need. Usually, demonization is used for these purposes, but more often it is enough to do it in a simpler way.

Add to cron:

This line:

@reboot ~ / _scripts / cron-autorun-reboot.sh> ~ / _scripts / cron-autorun-reboot.log 2> & 1

Don’t forget to make the script executable:

chmod + x ~ / _scripts / cron-autorun-reboot.sh

Script content:

As an example, here’s how to start celery after a server reboot.

#! / bin / bash

cd /home/ploshadka/ploshadka.net/
source venv/bin/activate

# Remove everything that might work before
pkill -nine -f tasks.updates.celery && pkill -nine -f celery

# Sometimes required for celery, otherwise errors will occur
export COLUMNS=80

# Add tasks to the scheduler
celery -A tasks.updates.celery beat –detach –config= configs.private.celery -l INFO –logfile= logs/beat.log

# Turn on worker
celery -A tasks.updates.celery worker -D –purge –config= configs.private.celery -l INFO –logfile= logs/celery.log

Errors

Error ‘source: not found’ may occur if file type is not specified or is specified #! / bin / sh instead #! / bin / bash

Similar Posts

Leave a Reply