C code adapted from a tutorial.
Suspend:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
#define word "mem"
// creating file pointer to work with files
FILE *fptr;
setuid(0);
// opening file in writing mode
fptr = fopen("/sys/power/state", "w");
// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr, "%s", word);
fclose(fptr);
return 0;
}
Hibernate
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
#define word1 "disk"
#define word2 "platform"
// creating file pointer to work with files
FILE *fptr;
FILE *fptr2;
setuid(0);
// opening file in writing mode
fptr2 = fopen("/sys/power/disk", "w");
fptr = fopen("/sys/power/state", "w");
fprintf(fptr2, "%s", word2);
fprintf(fptr, "%s", word1);
fclose(fptr2);
fclose(fptr);
return 0;
}
For example save the suspend one as mysuspend.c and compile
cc mysuspend.c -o mysuspend
Set SUID
sudo chown root:root mysuspend
sudo chmod u+s mysuspend
Now running that program as a normal user will suspend.
What I do is copy them to /usr/local/bin and I've set up KDE keyboard shortcuts to point at both (CTRL-ALT-SHIFT-s for suspend)
Ideally you'd want to find what is run when the lid is closed and intercept it , redirect it, to mysuspend.
Maybe someone who uses a laptop regularly will know how ?