0

How the rc.local can be used to resume the script after resume?

Some explanation from user's comment:

Boot_desired_kernel() {} 
# after this system will reboot 
Do_task(){} 
Do_task() /*

Using the pasting the file in init.d and updating it is working for script to run after reboot but what I am expecting is the script should start where it left off before reboot

1
  • How the rc.local can be used to resume the script after system reboot? Mar 24, 2020 at 10:56

2 Answers 2

1

I don't know, how to do it using rc.local, but maybe you will be satisfied with systemd way:

  • Create the new systemd service:

    sudo vim.tiny /etc/systemd/system/startscript.service
    

    You could nano instead of vim.tiny or other editor you like
    Paste the next to it:

    [Unit]
    Description=Start script after resume
    After=suspend.target
    
    [Service]
    Type=simple
    ExecStart=/full/path/to/the/script.sh
    
    [Install]
    WantedBy=suspend.target
    
  • Enable and start the service:

    sudo systemctl enable startscript && sudo systemctl start startscript
    
4
  • Using the pasting the file in init.d and updating it is working for script to run after reboot but what I am expecting is the script should start where it left off before reboot Apr 11, 2020 at 18:22
  • Show an example of script you want to resume. Does Resume in your case is reboot?
    – Gryu
    Apr 12, 2020 at 4:54
  • Boot_desired_kernel() {} # after this system will reboot Do_task(){} Do_task() /* Apr 13, 2020 at 7:02
  • Resume and reboot are very different things. As I see you want to reboot into a specific kernel and run some commands just after rebooting.
    – Gryu
    Apr 13, 2020 at 8:26
0

I don't know how to resume script execution after reboot, but you could check kernel version and if the kernel version is what you need, you could execute the rest of the script skipping the kernel changing and rebooting part as in the next script. But the script will be executed before the first reboot and after each reboot.

#!/bin/bash

kernel="5.3.0-40-generic"

if [[ $(uname -a) != *$kernel* ]]; then
    kernlist="$(grep -i "menuentry '" /boot/grub/grub.cfg|sed -r "s|--class .*$||g")"
    printf "%s$kernlist\n" | logger
    menuline="$(printf "%s$kernlist\n"|grep -ne $kernel | grep -v recovery | cut -f1 -d":")"
    menunum="$(($menuline-2))"
    grub-reboot "1>$menunum"
    echo "The next grub's menu entry will be choosen after the reboot:\n 1>$menunum" | logger
    reboot
fi

if [[ $(uname -a) == *$kernel* ]];
  then echo "This command is executed after reboot to $kernel" | logger;
fi

echo "Currently running kernel version is: $(uname -r)" | logger

After reboot it skips running kernel selecting part if the current kernel is of required version and echoes "This command is executed after reboot to KERNEL_VERSION_HERE" if you run journalctl -b | grep This or:

$ journalctl -b | grep Currently
Apr 13 17:35:51 lenovo root[1013]: Currently running kernel version is: 5.3.0-40-generic

After adding this to systemd it loads with default kernel and then automatically reboots and boots with 5.3.0-40-generic kernel.

$ cat /etc/systemd/system/resscript.service 
[Unit]
Description=Start script after reboot
After=network.target

[Service]
Type=simple
ExecStart=/home/user/test/resscript.sh

[Install]
WantedBy=multi-user.target
3
  • Thank you for the input , I try to work this out Apr 14, 2020 at 9:20
  • Tried the same exact step , seems to be not working for me Apr 16, 2020 at 8:18
  • Make sure you have spaces in if statements like this: $(uname -a) == *$kernel*. If You've edited the text without spaces before and after == it will not work: $(uname -a)==*$kernel* - don't do so. And also, what journalctl -b | grep Currently shows in your case?
    – Gryu
    Apr 16, 2020 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .